Skip to content

Commit 331d6e3

Browse files
committed
pr: addresses pr feedback.
1 parent 24b0467 commit 331d6e3

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

google/api_core/exceptions.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class GoogleAPICallError(GoogleAPIError, metaclass=_GoogleAPICallErrorMeta):
105105
response (Union[requests.Request, grpc.Call]): The response or
106106
gRPC call metadata.
107107
error_info (Union[error_details_pb2.ErrorInfo, None]): An optional object containing error info
108-
(google.rpc.error_details.ErrorInfo)
108+
(google.rpc.error_details.ErrorInfo).
109109
"""
110110

111111
code: Union[int, None] = None
@@ -149,15 +149,46 @@ def errors(self):
149149
return list(self._errors)
150150

151151
@property
152-
def error_info(self):
153-
"""Information contained in google.rpc.error_details.ErrorInfo
152+
def reason(self):
153+
"""The reason of the error.
154154
155155
Reference:
156156
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
157+
158+
Returns:
159+
Union[str, None]: An optional string containing reason of the error.
160+
"""
161+
if not self._error_info:
162+
return None
163+
return self._error_info.reason
164+
165+
@property
166+
def domain(self):
167+
"""The logical grouping to which the "reason" belongs.
168+
169+
Reference:
170+
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
171+
172+
Returns:
173+
Union[str, None]: An optional string containing a logical grouping to which the "reason" belongs.
174+
"""
175+
if not self._error_info:
176+
return None
177+
return self._error_info.domain
178+
179+
@property
180+
def metadata(self):
181+
"""Additional structured details about this error
182+
183+
Reference:
184+
https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L112
185+
157186
Returns:
158-
Union[error_details_pb2.ErrorInfo, None]: An optional object containing google.rpc.error_details.ErrorInfo.
187+
Union[Dict[str, str], None]: An optional object containing structured details about the error.
159188
"""
160-
return self._error_info
189+
if not self._error_info:
190+
return None
191+
return self._error_info.metadata
161192

162193
@property
163194
def details(self):

tests/unit/test_exceptions.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ def test_error_details_from_rest_response():
302302

303303
# See JSON schema in https://cloud.google.com/apis/design/errors#http_mapping
304304
http_response = make_response(
305-
json.dumps({"error": json.loads(json_format.MessageToJson(status, sort_keys=True))}).encode(
306-
"utf-8"
307-
)
305+
json.dumps(
306+
{"error": json.loads(json_format.MessageToJson(status, sort_keys=True))}
307+
).encode("utf-8")
308308
)
309309
exception = exceptions.from_http_response(http_response)
310310
want_error_details = [
@@ -336,7 +336,11 @@ def test_error_details_from_v1_rest_response():
336336
)
337337
exception = exceptions.from_http_response(response)
338338
assert exception.details == []
339-
assert exception.error_info is None
339+
assert (
340+
exception.reason is None
341+
and exception.domain is None
342+
and exception.metadata is None
343+
)
340344

341345

342346
@pytest.mark.skipif(grpc is None, reason="gRPC not importable")
@@ -362,7 +366,9 @@ def test_error_details_from_grpc_response():
362366
status_br_detail.Unpack(bad_request_detail)
363367
status_ei_detail.Unpack(error_info_detail)
364368
assert exception.details == [bad_request_detail, error_info_detail]
365-
assert exception.error_info == error_info_detail
369+
assert exception.reason == error_info_detail.reason
370+
assert exception.domain == error_info_detail.domain
371+
assert exception.metadata == error_info_detail.metadata
366372

367373

368374
@pytest.mark.skipif(grpc is None, reason="gRPC not importable")
@@ -381,4 +387,8 @@ def test_error_details_from_grpc_response_unknown_error():
381387
m.return_value = status
382388
exception = exceptions.from_grpc_error(error)
383389
assert exception.details == [status_detail]
384-
assert exception.error_info is None
390+
assert (
391+
exception.reason is None
392+
and exception.domain is None
393+
and exception.metadata is None
394+
)

0 commit comments

Comments
 (0)