Skip to content

Commit 573413a

Browse files
committed
avoid wrapping errors for rest callable
1 parent 2fc2b35 commit 573413a

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
return error
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+
return message
478486

479-
def from_http_response(response):
480-
"""Create a :class:`GoogleAPICallError` from a :class:`requests.Response`.
487+
def format_http_response_error(response, method, url, payload=None):
488+
"""Create a :class:`GoogleAPICallError` from a google 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-
except ValueError:
493-
payload = {"error": {"message": response.text or "unknown error"}}
494-
501+
payload = {} if not payload else payload
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] if error_info else None
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
return exception
523525

524526

527+
def from_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+
except ValueError:
541+
payload = {"error": {"message": response.text or "unknown error"}}
542+
return format_http_response_error(response, response.request.method, response.request.url, payload)
543+
544+
525545
def exception_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
import functools
2121

22-
from google.api_core import grpc_helpers_async, rest_helpers_async
22+
from google.api_core import grpc_helpers_async
2323
from google.api_core.gapic_v1 import client_info
2424
from google.api_core.gapic_v1.method import _GapicCallable
2525
from google.api_core.gapic_v1.method import DEFAULT # 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-
if kind == "rest":
45-
func = rest_helpers_async.wrap_errors(func)
46-
else:
44+
if kind == "grpc":
4745
func = grpc_helpers_async.wrap_errors(func)
4846

4947
metadata = [client_info.to_grpc_metadata()] if client_info is not None else None

google/api_core/rest_helpers_async.py

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy