Skip to content

Commit

Permalink
NO-ISSUE Enhance Documentation for Response and Error Handling in SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang-33 committed Mar 4, 2024
1 parent 54f0891 commit b87a71f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
24 changes: 22 additions & 2 deletions docs/guide/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,32 @@ if (event.type === 'message') {
For more detail of building webhook and retrieve event objects, please refer to
its [guide](./webhook.md).

## How to get response header and HTTP status code
You may need to store the ```x-line-request-id``` header obtained as a response from several APIs.
In this case, please use ```~WithHttpInfo``` functions. You can get headers and status codes.
The ```x-line-accepted-request-id``` or ```content-type``` header can also be obtained in the same way.

``` js
client
.replyMessageWithHttpInfo({
replyToken: replyToken,
messages: [message]
})
.then((response) => {
console.log(response.httpResponse.headers.get('x-line-request-id'));
console.log(response.httpResponse.status);
});
```

## Error handling

There are 4 types of errors caused by client usage.

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

For methods returning `Promise`, you can handle the errors
Expand All @@ -92,8 +110,10 @@ client
messages: [message]
})
.catch((err) => {
if (err instanceof HTTPError) {
if (err instanceof HTTPFetchError) {
console.error(err.statusCode);
console.error(err.headers.get('x-line-request-id'));
console.error(err.body);
}
});

Expand Down
9 changes: 7 additions & 2 deletions examples/echo-bot-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
middleware,
MiddlewareConfig,
webhook,
HTTPFetchError,
} from '@line/bot-sdk';
import express, {Application, Request, Response} from 'express';

Expand Down Expand Up @@ -76,7 +77,11 @@ app.post(
try {
await textEventHandler(event);
} catch (err: unknown) {
if (err instanceof Error) {
if (err instanceof HTTPFetchError) {
console.error(err.statusCode);
console.error(err.headers.get('x-line-request-id'));
console.error(err.body);
} else if (err instanceof Error) {
console.error(err);
}

Expand All @@ -88,7 +93,7 @@ app.post(
})
);

// Return a successfull message.
// Return a successful message.
return res.status(200).json({
status: 'success',
results,
Expand Down

0 comments on commit b87a71f

Please sign in to comment.