-
-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raise HTTPError in case of HTTP 5XX responses. #441
Open
ab
wants to merge
7
commits into
requests:master
Choose a base branch
from
ab:raise-5xx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eea0f5b
Raise HTTPError in case of HTTP 5XX responses.
ab bb90460
Fix tests.
ab ba0e5d2
Add tests of 5xx error handling.
ab 1e0f279
Move type annotation to comment for Py2 compat.
ab 073e46a
Test byte decoding of _raise_for_5xx.
ab a560d84
Appease black.
ab fa78265
Fix black formatting of existing code.
ab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from oauthlib.oauth2 import LegacyApplicationClient | ||
from oauthlib.oauth2 import TokenExpiredError, is_secure_transport | ||
import requests | ||
from requests.exceptions import HTTPError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
@@ -230,9 +231,9 @@ def fetch_token( | |
`auth` tuple. If the value is `None`, it will be | ||
omitted from the request, however if the value is | ||
an empty string, an empty string will be sent. | ||
:param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client | ||
Authentication (draft-ietf-oauth-mtls). Can either be the | ||
path of a file containing the private key and certificate or | ||
:param cert: Client certificate to send for OAuth 2.0 Mutual-TLS Client | ||
Authentication (draft-ietf-oauth-mtls). Can either be the | ||
path of a file containing the private key and certificate or | ||
a tuple of two filenames for certificate and key. | ||
:param kwargs: Extra parameters to include in the token request. | ||
:return: A token dict | ||
|
@@ -363,6 +364,8 @@ def fetch_token( | |
log.debug("Invoking hook %s.", hook) | ||
r = hook(r) | ||
|
||
self._raise_for_5xx(response=r) | ||
|
||
self._client.parse_request_body_response(r.text, scope=self.scope) | ||
self.token = self._client.token | ||
log.debug("Obtained token %s.", self.token) | ||
|
@@ -449,6 +452,8 @@ def refresh_token( | |
log.debug("Invoking hook %s.", hook) | ||
r = hook(r) | ||
|
||
self._raise_for_5xx(response=r) | ||
|
||
self.token = self._client.parse_request_body_response(r.text, scope=self.scope) | ||
if not "refresh_token" in self.token: | ||
log.debug("No new refresh token given. Re-using old.") | ||
|
@@ -538,3 +543,40 @@ def register_compliance_hook(self, hook_type, hook): | |
"Hook type %s is not in %s.", hook_type, self.compliance_hook | ||
) | ||
self.compliance_hook[hook_type].add(hook) | ||
|
||
def _raise_for_5xx(self, response): | ||
# type: (requests.models.Response) -> None | ||
""" | ||
Raise requests.HTTPError if response is an HTTP 5XX error. | ||
|
||
Just like the existing Response.raise_for_status() but ignores 4XX | ||
errors. | ||
|
||
:param response: HTTP response object from requests | ||
Raises :class:`requests.exceptions.HTTPError`, if a 5XX error occurred. | ||
""" | ||
http_error_msg = "" | ||
if isinstance(response.reason, bytes): | ||
# We attempt to decode utf-8 first because some servers | ||
# choose to localize their reason strings. If the string | ||
# isn't utf-8, we fall back to iso-8859-1 for all other | ||
# encodings. (See psf/requests PR #3538) | ||
try: | ||
reason = response.reason.decode("utf-8") | ||
except UnicodeDecodeError: | ||
reason = response.reason.decode("iso-8859-1") | ||
else: | ||
reason = response.reason | ||
|
||
if 400 <= response.status_code < 500: | ||
pass # ignored | ||
|
||
elif 500 <= response.status_code < 600: | ||
http_error_msg = "%s Server Error: %s for url: %s" % ( | ||
response.status_code, | ||
reason, | ||
response.url, | ||
) | ||
|
||
if http_error_msg: | ||
raise HTTPError(http_error_msg, response=response) | ||
Comment on lines
+581
to
+582
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only define |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to only need the
reason
in theelif 500 <= response.status_code < 600
block below, so maybe worth moving it in there? That way the behavior doesn't change for any other response codes (at the moment you try decode the reason even if ultimately you might not need it, thus introducing potential risk of breakage for no reason.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is from
requests.Response.raise_for_status()
, and I chose to make as few changes as possible since the only desired difference in behavior is to ignore 4XX errors.