- Notifications
You must be signed in to change notification settings - Fork90
feat: Add support for creating exceptions from an asynchronous response#688
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
2fc2b35
feat: add suport for mapping exceptions to rest callables
ohmayr573413a
avoid wrapping errors for rest callable
ohmayr9c76b5f
fix lint issues
ohmayr2373149
add test coverage
ohmayrdd7b5d1
address PR comments
ohmayrf7ebadd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot]be83346
fix lint issues
ohmayrb477772
Merge branch 'add-support-for-mapping-rest-callables' of github.com:g…
ohmayrcd13bdf
Merge branch 'main' into add-support-for-mapping-rest-callables
ohmayr324a2a0
fix for none type method
ohmayrcabb338
Merge branch 'add-support-for-mapping-rest-callables' of github.com:g…
ohmayrFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
vchudnov-g marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -22,7 +22,7 @@ | ||
from __future__ import unicode_literals | ||
import http.client | ||
from typing importOptional,Dict | ||
from typing import Union | ||
import warnings | ||
@@ -476,22 +476,37 @@ def from_http_status(status_code, message, **kwargs): | ||
return error | ||
def _format_rest_error_message(error, method, url): | ||
method = method.upper() if method else None | ||
message = "{method} {url}: {error}".format( | ||
method=method, | ||
url=url, | ||
ohmayr marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
error=error, | ||
) | ||
return message | ||
# NOTE: We're moving away from `from_http_status` because it expects an aiohttp response compared | ||
# to `format_http_response_error` which expects a more abstract response from google.auth and is | ||
# compatible with both sync and async response types. | ||
# TODO(https://github.com/googleapis/python-api-core/issues/691): Add type hint for response. | ||
def format_http_response_error( | ||
response, method: str, url: str, payload: Optional[Dict] = None | ||
): | ||
"""Create a :class:`GoogleAPICallError` from a google auth rest response. | ||
Args: | ||
response Union[google.auth.transport.Response, google.auth.aio.transport.Response]: The HTTP response. | ||
method Optional(str): The HTTP request method. | ||
url Optional(str): The HTTP request url. | ||
payload Optional(dict): The HTTP response payload. If not passed in, it is read from response for a response type of google.auth.transport.Response. | ||
Returns: | ||
GoogleAPICallError: An instance of the appropriate subclass of | ||
:class:`GoogleAPICallError`, with the message and errors populated | ||
from the response. | ||
""" | ||
payload = {} if not payload else payload | ||
error_message = payload.get("error", {}).get("message", "unknown error") | ||
errors = payload.get("error", {}).get("errors", ()) | ||
# In JSON, details are already formatted in developer-friendly way. | ||
@@ -504,12 +519,7 @@ def from_http_response(response): | ||
) | ||
) | ||
error_info = error_info[0] if error_info else None | ||
message = _format_rest_error_message(error_message, method, url) | ||
exception = from_http_status( | ||
response.status_code, | ||
@@ -522,6 +532,26 @@ def from_http_response(response): | ||
return exception | ||
def from_http_response(response): | ||
"""Create a :class:`GoogleAPICallError` from a :class:`requests.Response`. | ||
Args: | ||
response (requests.Response): The HTTP response. | ||
Returns: | ||
GoogleAPICallError: An instance of the appropriate subclass of | ||
:class:`GoogleAPICallError`, with the message and errors populated | ||
from the response. | ||
""" | ||
try: | ||
payload = response.json() | ||
except ValueError: | ||
payload = {"error": {"message": response.text or "unknown error"}} | ||
return format_http_response_error( | ||
response, response.request.method, response.request.url, payload | ||
) | ||
def exception_class_for_grpc_status(status_code): | ||
"""Return the exception class for a specific :class:`grpc.StatusCode`. | ||
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
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
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.