Skip to content

Commit 6e7fc4e

Browse files
authoredDec 26, 2023
Enhance Documentation for Response and Error Handling in SDK (#579)
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-python.
1 parent 466ab86 commit 6e7fc4e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
 

‎README.rst

+38
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,44 @@ Thus, you can send a JSON designed with `Flex Message Simulator <https://develop
283283
)
284284
)
285285
286+
How to get x-line-request-id header and error message
287+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288+
289+
You may need to store the ``x-line-request-id`` header obtained as a response from several APIs.
290+
In this case, please use ``~_with_http_info`` functions. You can get headers and status codes.
291+
The ``x-line-accepted-request-id`` or ``content-type`` header can also be obtained in the same way.
292+
293+
.. code:: python
294+
295+
response = line_bot_api.reply_message_with_http_info(
296+
ReplyMessageRequest(
297+
reply_token=event.reply_token,
298+
messages=[TextMessage(text='see application log')]
299+
)
300+
)
301+
app.logger.info("Got response with http status code: " + str(response.status_code))
302+
app.logger.info("Got x-line-request-id: " + response.headers['x-line-request-id'])
303+
app.logger.info("Got response with http body: " + str(response.data))
304+
305+
You can get error messages from ``ApiException`` when you use ``MessagingApi``. Each client defines its own exception class.
306+
307+
.. code:: python
308+
309+
from linebot.v3.messaging import ApiException, ErrorResponse
310+
try:
311+
line_bot_api.reply_message_with_http_info(
312+
ReplyMessageRequest(
313+
reply_token='invalid-reply-token',
314+
messages=[TextMessage(text='see application log')]
315+
)
316+
)
317+
except ApiException as e:
318+
app.logger.info("Got response with http status code: " + str(e.status))
319+
app.logger.info("Got x-line-request-id: " + e.headers['x-line-request-id'])
320+
app.logger.info("Got response with http body: " + str(ErrorResponse.from_json(e.body)))
321+
322+
When you need to get ``x-line-accepted-request-id`` header from error response, you can get it: ``e.headers['x-line-accepted-request-id']``.
323+
286324

287325
Help and media
288326
--------------

‎examples/flask-kitchensink/app.py

+23
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
CameraAction,
9595
CameraRollAction,
9696
LocationAction,
97+
ErrorResponse
9798
)
9899

99100
from linebot.v3.insight import (
@@ -698,6 +699,28 @@ def handle_text_message(event):
698699
messages=messages
699700
)
700701
)
702+
elif text == 'with http info':
703+
response = line_bot_api.reply_message_with_http_info(
704+
ReplyMessageRequest(
705+
reply_token=event.reply_token,
706+
messages=[TextMessage(text='see application log')]
707+
)
708+
)
709+
app.logger.info("Got response with http status code: " + str(response.status_code))
710+
app.logger.info("Got x-line-request-id: " + response.headers['x-line-request-id'])
711+
app.logger.info("Got response with http body: " + str(response.data))
712+
elif text == 'with http info error':
713+
try:
714+
line_bot_api.reply_message_with_http_info(
715+
ReplyMessageRequest(
716+
reply_token='invalid-reply-token',
717+
messages=[TextMessage(text='see application log')]
718+
)
719+
)
720+
except ApiException as e:
721+
app.logger.info("Got response with http status code: " + str(e.status))
722+
app.logger.info("Got x-line-request-id: " + e.headers['x-line-request-id'])
723+
app.logger.info("Got response with http body: " + str(ErrorResponse.from_json(e.body)))
701724
else:
702725
line_bot_api.reply_message(
703726
ReplyMessageRequest(

0 commit comments

Comments
 (0)
Please sign in to comment.