Improve Informativeness of Network Errors#4822
Merged
Bibo-Joshi merged 4 commits intomasterfrom Jun 29, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances network error handling by using HTTPStatus.phrase for more descriptive messages and chaining JSON parsing errors to provide context.
- Improved test coverage to validate new error messages and parsing failure behavior
- Updated
_request_wrapperto build messages fromHTTPStatus.phrase, wrapparse_json_payloadintry/except, and chain exceptions - Added changelog entry for the feature
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/request/test_request.py | Adjusted tests to check for descriptive HTTP status phrases and JSON parse errors |
| src/telegram/request/_baserequest.py | Enhanced error message construction, used HTTPStatus phrases, and chained parsing exceptions |
| changes/unreleased/4822.DrW3tJ3KoB8kTmHtNnNEpQ.toml | Added changelog entry for this PR |
Comments suppressed due to low confidence (2)
src/telegram/request/_baserequest.py:355
- [nitpick] The variable name
exceptionshadows the built-inexceptionconcept. Consider renaming it to something likeerrortelegram_errorfor clarity.
exception: TelegramError = Forbidden(message)
src/telegram/request/_baserequest.py:318
- [nitpick] Python 3.12 introduces
HTTPStatus.is_success, which can replace the manual range check for success codes to make the intent clearer and the code more maintainable.
if HTTPStatus.OK <= code <= 299:
harshil21
reviewed
Jun 14, 2025
Member
harshil21
left a comment
There was a problem hiding this comment.
just another try-except! :D
harshil21
approved these changes
Jun 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on observations in #4821
HTTPStatus.phraseas more descriptive error message textparse_json_payloadin a try-except to add information about failing to parse the server response while still raising the appropriate exception