Docs: "Handling Exceptions" should mention that network errors are not `TwilioRestException`
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 1/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 90/100
- issue の種類
- ドキュメント
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
調査の方向性
README.md の「Handling Exceptions」セクションから始め、twilio/base/exceptions.py、twilio/http/http_client.py、twilio/http/async_http_client.py で動作を確認してください。README を更新して、API のエラーレスポンスとトランスポート例外を区別し、それらを捕捉する方法を示してください。完了の条件は、例がドキュメントに記載された同期および非同期の動作と一致することです。
索引モデルが issue の本文から書いたものです。
説明
Location (twilio/twilio-python @ default branch 74b9629):
README.md:267-284twilio/base/exceptions.py:10-11twilio/http/http_client.py:101-111twilio/http/async_http_client.py:104-110
try:
message = client.messages.create(to="+12316851234", from_="+15555555555",
body="Hello there!")
except TwilioRestException as e:
print(e)
# twilio/http/http_client.py
response = session.send(
prepped_request,
allow_redirects=allow_redirects,
timeout=timeout,
**settings,
)
Problem
The README's "Handling Exceptions" section shows only except TwilioRestException. TwilioRestException is documented as "A generic 400 or 500 level exception from the Twilio API", i.e. it covers responses returned by the API. When the request never gets a response (DNS failure, connection refused, timeout) the default TwilioHttpClient lets the requests exception propagate as-is, so requests.exceptions.ConnectionError / requests.exceptions.Timeout escape that handler. The README doesn't mention this, so code written from the example can miss transport failures entirely.
I think propagating the original requests exception is a reasonable design: it keeps the exact cause and lets callers distinguish timeouts (possibly retryable) from other errors. This request is only about documenting it.
Reproduction
With session.send raising, TwilioHttpClient.request() raises the original exception (observed with mocked Session.send):
requests.exceptions.ConnectionError: Connection refused
requests.exceptions.Timeout: execution expired
Environment:
- Commit:
74b96296b0805986252b798bda4bca90341487ee(main, twilio 9.12.0) - Toolchain: Python 3.13.18
Already fixed in twilio/twilio-ruby
PR: https://github.com/twilio/twilio-ruby/pull/470 — "breaking: Catch and wrap Faraday client errors" (merged 2019-09-16T16:35:47Z, merge commit a5d428281af). The same gap was addressed in the Ruby library, but by a different route: it wraps Faraday client errors in Twilio::REST::TwilioError and adds a "Handling Errors" README section, as a breaking change. The wrapping is shown for context and is not proposed here; only making the behaviour explicit to users is.
How twilio-ruby fixed it:
--- a/lib/twilio-ruby/http/http_client.rb
+++ b/lib/twilio-ruby/http/http_client.rb
@@ -52,6 +50,14 @@ def _request(request)
...
+ def send(request)
+ @connection.send(request.method.downcase.to_sym,
+ request.url,
+ request.method == 'GET' ? request.params : request.data)
+ rescue Faraday::ClientError => e
+ raise Twilio::REST::TwilioError, e
+ end
--- a/README.md
+++ b/README.md
@@ -98,30 +98,38 @@ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
...
+### Handling Errors
...
+```ruby
+begin
+ messages = @client.messages.list(limit: 20)
+rescue Twilio::REST::TwilioError => e
+ puts e.message
+end
+```
Suggested fix
Docs only: after the "Handling Exceptions" example, the README states that TwilioRestException covers API error responses, that transport failures surface as requests (sync) or aiohttp (async) exceptions, and shows how to catch them; wrapping transport errors or adding retries is not proposed:
diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -283,6 +283,23 @@ except TwilioRestException as e:
print(e)
```
+`TwilioRestException` represents an error response returned by the Twilio API. If the request gets no response at all (for example a DNS failure, a refused connection or a timeout), the default `TwilioHttpClient` lets the underlying `requests` exception propagate, so `requests.exceptions.ConnectionError`, `requests.exceptions.Timeout` and the other `requests.exceptions.RequestException` subclasses are not caught by `except TwilioRestException`. The async client (`AsyncTwilioHttpClient`) raises aiohttp's exceptions in the same situation. To handle these failures, catch them separately:
+
+```python
+from requests.exceptions import ConnectionError, Timeout
+from twilio.base.exceptions import TwilioRestException
+
+try:
+ message = client.messages.create(to="+12316851234", from_="+15555555555",
+ body="Hello there!")
+except TwilioRestException as e:
+ print(e.status, e.code) # the API returned an error
+except Timeout:
+ pass # no response in time; the outcome is unknown, decide whether to retry
+except ConnectionError:
+ pass # the API could not be reached
+```
+
### Generating TwiML
To control phone calls, your application needs to output [TwiML][twiml].
Documentation-only change, so there is no failing test; the documented behaviour was checked on 74b9629 (Python 3.13.18) with Session.send mocked to raise ConnectionError and Timeout: TwilioHttpClient.request() re-raises both unchanged, and neither is a TwilioRestException.
I can submit a PR for this.
Checked: #327 (hanging behind proxy), #485 (timeout parameter) and #630 (timeout type hint) don't cover how transport errors are surfaced or documented.
- 主要言語
- Python
- スター
- 2.1k
- フォーク
- 821
- 平均マージ
- 9時間 33分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
twilio/twilio-python のほかの issue
-
Clearer diagnostic when an API Key SID is passed without `account_sid`対応中かも @saryeljaramany が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
twilio/twilio-python#985 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
TokenPagination.next_page_async() makes a sync request and fails with an async http client対応中かも @sebxt が 31 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
twilio/twilio-python#976 ·
メンテナーはふだん 1 日以内に返信
-
AsyncTwilioHttpClient does not JSON-serialize request bodies for application/json endpoints (sync client does) → 400 on JSON-body v2 endpoints対応中かも @Steve0x2a が 77 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
twilio/twilio-python#932 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
twilio/twilio-python#930 ·
メンテナーはふだん 1 日以内に返信
-
[BUG] AsyncTwilioHttpClient.request does not use self.timeout対応中かも @Hashim1999164 が 96 日前に担当しました。 オープンtype: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
twilio/twilio-python#925 ·
メンテナーはふだん 1 日以内に返信
twilio/twilio-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
メンテナーはふだん 1 日以内に返信
-
area:cli area:security enhancement priority:medium size:small
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
RonaldHensbergen/composable-data-stack#1338 ·
メンテナーはふだん 1 日以内に返信
-
area:stdlib effort:S impact:med platform:linux type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
automated endpoint-dead needs-curation
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
almanac-data/climate-almanac#139 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
UKGovernmentBEIS/inspect_ai#6695 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信