-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add support for 'error_info' #307
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db97527
feat: Adds support for error_info.
atulep 7c02189
Merge branch 'main' into feat_error_details2
atulep cb6ead7
chore: fixes docstring.
atulep 626b992
Update google/api_core/exceptions.py
atulep 24b0467
chore: fixes flaky test.
atulep 331d6e3
pr: addresses pr feedback.
atulep d62a0dd
Merge branch 'main' into feat_error_details2
atulep 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 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 |
---|---|---|
|
@@ -104,6 +104,8 @@ class GoogleAPICallError(GoogleAPIError, metaclass=_GoogleAPICallErrorMeta): | |
details (Sequence[Any]): An optional list of objects defined in google.rpc.error_details. | ||
response (Union[requests.Request, grpc.Call]): The response or | ||
gRPC call metadata. | ||
error_info (Union[error_details_pb2.ErrorInfo, None]): An optional object containing error info | ||
(google.rpc.error_details.ErrorInfo) | ||
""" | ||
|
||
code: Union[int, None] = None | ||
|
@@ -122,12 +124,13 @@ class GoogleAPICallError(GoogleAPIError, metaclass=_GoogleAPICallErrorMeta): | |
This may be ``None`` if the exception does not match up to a gRPC error. | ||
""" | ||
|
||
def __init__(self, message, errors=(), details=(), response=None): | ||
def __init__(self, message, errors=(), details=(), response=None, error_info=None): | ||
super(GoogleAPICallError, self).__init__(message) | ||
self.message = message | ||
"""str: The exception message.""" | ||
self._errors = errors | ||
self._details = details | ||
self._error_info = error_info | ||
self._response = response | ||
|
||
def __str__(self): | ||
|
@@ -145,6 +148,17 @@ def errors(self): | |
""" | ||
return list(self._errors) | ||
|
||
@property | ||
def error_info(self): | ||
"""Information contained in google.rpc.error_details.ErrorInfo | ||
|
||
Reference: | ||
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112 | ||
tseaver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Returns: | ||
Union[error_details_pb2.ErrorInfo, None]: An optional object containing google.rpc.error_details.ErrorInfo. | ||
""" | ||
return self._error_info | ||
|
||
@property | ||
def details(self): | ||
"""Information contained in google.rpc.status.details. | ||
|
@@ -433,13 +447,25 @@ def from_http_response(response): | |
errors = payload.get("error", {}).get("errors", ()) | ||
# In JSON, details are already formatted in developer-friendly way. | ||
details = payload.get("error", {}).get("details", ()) | ||
|
||
error_info = list( | ||
filter( | ||
lambda detail: detail.get("@type", "") | ||
== "type.googleapis.com/google.rpc.ErrorInfo", | ||
details, | ||
) | ||
) | ||
Comment on lines
+481
to
+487
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. Nit: it's more idiomatic to use list comprehensions than error_info_type = "type.googleapis.com/google.rpc.ErrorInfo"
error_info = [d for d in details if d.get("@type", "") == error_info_type] |
||
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 | ||
) | ||
|
||
exception = from_http_status( | ||
response.status_code, message, errors=errors, details=details, response=response | ||
response.status_code, | ||
message, | ||
errors=errors, | ||
details=details, | ||
response=response, | ||
error_info=error_info, | ||
) | ||
return exception | ||
|
||
|
@@ -490,10 +516,10 @@ def _parse_grpc_error_details(rpc_exc): | |
try: | ||
status = rpc_status.from_call(rpc_exc) | ||
except NotImplementedError: # workaround | ||
return [] | ||
return [], None | ||
|
||
if not status: | ||
return [] | ||
return [], None | ||
|
||
possible_errors = [ | ||
error_details_pb2.BadRequest, | ||
|
@@ -507,6 +533,7 @@ def _parse_grpc_error_details(rpc_exc): | |
error_details_pb2.Help, | ||
error_details_pb2.LocalizedMessage, | ||
] | ||
error_info = None | ||
error_details = [] | ||
for detail in status.details: | ||
matched_detail_cls = list( | ||
|
@@ -519,7 +546,9 @@ def _parse_grpc_error_details(rpc_exc): | |
info = matched_detail_cls[0]() | ||
detail.Unpack(info) | ||
error_details.append(info) | ||
return error_details | ||
if isinstance(info, error_details_pb2.ErrorInfo): | ||
error_info = info | ||
return error_details, error_info | ||
|
||
|
||
def from_grpc_error(rpc_exc): | ||
|
@@ -535,12 +564,14 @@ def from_grpc_error(rpc_exc): | |
# NOTE(lidiz) All gRPC error shares the parent class grpc.RpcError. | ||
# However, check for grpc.RpcError breaks backward compatibility. | ||
if isinstance(rpc_exc, grpc.Call) or _is_informative_grpc_error(rpc_exc): | ||
details, err_info = _parse_grpc_error_details(rpc_exc) | ||
return from_grpc_status( | ||
rpc_exc.code(), | ||
rpc_exc.details(), | ||
errors=(rpc_exc,), | ||
details=_parse_grpc_error_details(rpc_exc), | ||
details=details, | ||
response=rpc_exc, | ||
error_info=err_info, | ||
) | ||
else: | ||
return GoogleAPICallError(str(rpc_exc), errors=(rpc_exc,), response=rpc_exc) |
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.