-
-
Couldn't load subscription status.
- Fork 426
Raise HTTPError in case of HTTP 5XX responses. #441
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
base: master
Are you sure you want to change the base?
Changes from all commits
eea0f5b
bb90460
ba0e5d2
1e0f279
073e46a
a560d84
fa78265
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
||
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
reasonin theelif 500 <= response.status_code < 600block 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.