Skip to content

Commit

Permalink
avoid wrapping errors for rest callable
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmayr committed Aug 23, 2024
1 parent 2fc2b35 commit 573413a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 72 deletions.
48 changes: 34 additions & 14 deletions google/api_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,30 @@ def from_http_status(status_code, message, **kwargs):

return error

def _format_error_message(error, method, url):
method = method.upper()
message = "{method} {url}: {error}".format(
method=method,
url=url,
error=error,
)
return message

def from_http_response(response):
"""Create a :class:`GoogleAPICallError` from a :class:`requests.Response`.
def format_http_response_error(response, method, url, payload=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(str): 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.
Expand All @@ -504,12 +511,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_error_message(error_message, method, url)

exception = from_http_status(
response.status_code,
Expand All @@ -522,6 +524,24 @@ 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`.
Expand Down
6 changes: 2 additions & 4 deletions google/api_core/gapic_v1/method_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import functools

from google.api_core import grpc_helpers_async, rest_helpers_async
from google.api_core import grpc_helpers_async
from google.api_core.gapic_v1 import client_info
from google.api_core.gapic_v1.method import _GapicCallable
from google.api_core.gapic_v1.method import DEFAULT # noqa: F401
Expand All @@ -41,9 +41,7 @@ def wrap_method(
and ``compression`` arguments and applies the common error mapping,
retry, timeout, metadata, and compression behavior to the low-level RPC method.
"""
if kind == "rest":
func = rest_helpers_async.wrap_errors(func)
else:
if kind == "grpc":
func = grpc_helpers_async.wrap_errors(func)

metadata = [client_info.to_grpc_metadata()] if client_info is not None else None
Expand Down
54 changes: 0 additions & 54 deletions google/api_core/rest_helpers_async.py

This file was deleted.

0 comments on commit 573413a

Please sign in to comment.