Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit573413a

Browse files
committed
avoid wrapping errors for rest callable
1 parent2fc2b35 commit573413a

File tree

3 files changed

+36
-72
lines changed

3 files changed

+36
-72
lines changed

‎google/api_core/exceptions.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -475,23 +475,30 @@ def from_http_status(status_code, message, **kwargs):
475475

476476
returnerror
477477

478+
def_format_error_message(error,method,url):
479+
method=method.upper()
480+
message="{method} {url}: {error}".format(
481+
method=method,
482+
url=url,
483+
error=error,
484+
)
485+
returnmessage
478486

479-
deffrom_http_response(response):
480-
"""Create a :class:`GoogleAPICallError` from a:class:`requests.Response`.
487+
defformat_http_response_error(response,method,url,payload=None):
488+
"""Create a :class:`GoogleAPICallError` from agoogle auth rest response.
481489
482490
Args:
483-
response (requests.Response): The HTTP response.
491+
response Union[google.auth.transport.Response, google.auth.aio.transport.Response]: The HTTP response.
492+
method Optional(str): The HTTP request method.
493+
url Optional(str): The HTTP request url.
494+
payload Optional(str): The HTTP response payload. If not passed in, it is read from response for a response type of google.auth.transport.Response.
484495
485496
Returns:
486497
GoogleAPICallError: An instance of the appropriate subclass of
487498
:class:`GoogleAPICallError`, with the message and errors populated
488499
from the response.
489500
"""
490-
try:
491-
payload=response.json()
492-
exceptValueError:
493-
payload= {"error": {"message":response.textor"unknown error"}}
494-
501+
payload= {}ifnotpayloadelsepayload
495502
error_message=payload.get("error", {}).get("message","unknown error")
496503
errors=payload.get("error", {}).get("errors", ())
497504
# In JSON, details are already formatted in developer-friendly way.
@@ -504,12 +511,7 @@ def from_http_response(response):
504511
)
505512
)
506513
error_info=error_info[0]iferror_infoelseNone
507-
508-
message="{method} {url}: {error}".format(
509-
method=response.request.method,
510-
url=response.request.url,
511-
error=error_message,
512-
)
514+
message=_format_error_message(error_message,method,url)
513515

514516
exception=from_http_status(
515517
response.status_code,
@@ -522,6 +524,24 @@ def from_http_response(response):
522524
returnexception
523525

524526

527+
deffrom_http_response(response):
528+
"""Create a :class:`GoogleAPICallError` from a :class:`requests.Response`.
529+
530+
Args:
531+
response (requests.Response): The HTTP response.
532+
533+
Returns:
534+
GoogleAPICallError: An instance of the appropriate subclass of
535+
:class:`GoogleAPICallError`, with the message and errors populated
536+
from the response.
537+
"""
538+
try:
539+
payload=response.json()
540+
exceptValueError:
541+
payload= {"error": {"message":response.textor"unknown error"}}
542+
returnformat_http_response_error(response,response.request.method,response.request.url,payload)
543+
544+
525545
defexception_class_for_grpc_status(status_code):
526546
"""Return the exception class for a specific :class:`grpc.StatusCode`.
527547

‎google/api_core/gapic_v1/method_async.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
importfunctools
2121

22-
fromgoogle.api_coreimportgrpc_helpers_async,rest_helpers_async
22+
fromgoogle.api_coreimportgrpc_helpers_async
2323
fromgoogle.api_core.gapic_v1importclient_info
2424
fromgoogle.api_core.gapic_v1.methodimport_GapicCallable
2525
fromgoogle.api_core.gapic_v1.methodimportDEFAULT# noqa: F401
@@ -41,9 +41,7 @@ def wrap_method(
4141
and ``compression`` arguments and applies the common error mapping,
4242
retry, timeout, metadata, and compression behavior to the low-level RPC method.
4343
"""
44-
ifkind=="rest":
45-
func=rest_helpers_async.wrap_errors(func)
46-
else:
44+
ifkind=="grpc":
4745
func=grpc_helpers_async.wrap_errors(func)
4846

4947
metadata= [client_info.to_grpc_metadata()]ifclient_infoisnotNoneelseNone

‎google/api_core/rest_helpers_async.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp