Skip to content

Commit b873ad6

Browse files
authored
Enhance Documentation for Response and Error Handling in SDK (#734)
This pull request updates the README and example projects to include detailed instructions on how to retrieve headers and HTTP status codes from API responses. Additionally, it outlines methods for capturing HTTP status codes, headers, and error details when an error occurs. These enhancements aim to improve the developer's understanding and handling of API interactions and error management within the line-bot-sdk-nodejs. (this depends on #733)
1 parent 2e59cad commit b873ad6

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

docs/guide/client.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,32 @@ if (event.type === 'message') {
7070
For more detail of building webhook and retrieve event objects, please refer to
7171
its [guide](./webhook.md).
7272

73+
## How to get response header and HTTP status code
74+
You may need to store the ```x-line-request-id``` header obtained as a response from several APIs.
75+
In this case, please use ```~WithHttpInfo``` functions. You can get headers and status codes.
76+
The ```x-line-accepted-request-id``` or ```content-type``` header can also be obtained in the same way.
77+
78+
``` js
79+
client
80+
.replyMessageWithHttpInfo({
81+
replyToken: replyToken,
82+
messages: [message]
83+
})
84+
.then((response) => {
85+
console.log(response.httpResponse.headers.get('x-line-request-id'));
86+
console.log(response.httpResponse.status);
87+
});
88+
```
89+
7390
## Error handling
7491

7592
There are 4 types of errors caused by client usage.
7693

7794
- `RequestError`: A request fails by, for example, wrong domain or server
7895
refusal.
7996
- `ReadError`: Reading from a response pipe fails.
80-
- `HTTPError`: Server returns a non-2xx response.
97+
- `HTTPFetchError`: Server returns a response with non-2xx HTTP status code.
98+
- (`HTTPError`: You get this error when you use deprecated client. This is not used in the maintained clients.)
8199
- `JSONParseError`: JSON parsing fails for response body.
82100

83101
For methods returning `Promise`, you can handle the errors
@@ -92,8 +110,10 @@ client
92110
messages: [message]
93111
})
94112
.catch((err) => {
95-
if (err instanceof HTTPError) {
113+
if (err instanceof HTTPFetchError) {
96114
console.error(err.statusCode);
115+
console.error(err.headers.get('x-line-request-id'));
116+
console.error(err.body);
97117
}
98118
});
99119

examples/echo-bot-ts/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
middleware,
77
MiddlewareConfig,
88
webhook,
9+
HTTPFetchError,
910
} from '@line/bot-sdk';
1011
import express, {Application, Request, Response} from 'express';
1112

@@ -76,7 +77,11 @@ app.post(
7677
try {
7778
await textEventHandler(event);
7879
} catch (err: unknown) {
79-
if (err instanceof Error) {
80+
if (err instanceof HTTPFetchError) {
81+
console.error(err.statusCode);
82+
console.error(err.headers.get('x-line-request-id'));
83+
console.error(err.body);
84+
} else if (err instanceof Error) {
8085
console.error(err);
8186
}
8287

@@ -88,7 +93,7 @@ app.post(
8893
})
8994
);
9095

91-
// Return a successfull message.
96+
// Return a successful message.
9297
return res.status(200).json({
9398
status: 'success',
9499
results,

0 commit comments

Comments
 (0)