Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 96
feat: Add support for creating exceptions from an asynchronous response#688
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2fc2b35
feat: add suport for mapping exceptions to rest callables
ohmayr 573413a
avoid wrapping errors for rest callable
ohmayr 9c76b5f
fix lint issues
ohmayr 2373149
add test coverage
ohmayr dd7b5d1
address PR comments
ohmayr f7ebadd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] be83346
fix lint issues
ohmayr b477772
Merge branch 'add-support-for-mapping-rest-callables' of github.com:g…
ohmayr cd13bdf
Merge branch 'main' into add-support-for-mapping-rest-callables
ohmayr 324a2a0
fix for none type method
ohmayr cabb338
Merge branch 'add-support-for-mapping-rest-callables' of github.com:g…
ohmayr File 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
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 import Dict | ||
| from typing import Optional, Dict | ||
| from typing import Union | ||
| import warnings | ||
| @@ -476,22 +476,37 @@ def from_http_status(status_code, message, **kwargs): | ||
| return error | ||
| def from_http_response(response): | ||
| """Create a :class:`GoogleAPICallError` from a :class:`requests.Response`. | ||
| 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.
Uh 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 (requests.Response): The HTTP response. | ||
| 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. | ||
| """ | ||
| try: | ||
| payload = response.json() | ||
| except ValueError: | ||
| payload = {"error": {"message": response.text or "unknown error"}} | ||
| 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 = "{method} {url}: {error}".format( | ||
| method=response.request.method, | ||
| url=response.request.url, | ||
| error=error_message, | ||
| ) | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.