From edb8c63e8a331f5e08ea19202d8de42de7051299 Mon Sep 17 00:00:00 2001 From: Victor Chudnovsky Date: Wed, 19 May 2021 15:34:48 -0700 Subject: [PATCH] feat: Raise GoogleAPICallError on REST response errors (#891) While testing against Showcase, I found that server errors resulted in a large exception stack dump that did not include the body of the erroring response. This PR shortens the stack dump and prints out the request body. --- .../%sub/services/%service/transports/rest.py.j2 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 index bf3cec5efc..afb296aeaf 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2 @@ -9,8 +9,9 @@ from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import operations_v1 {% endif %} from google.api_core import gapic_v1 # type: ignore -from google.auth import credentials as ga_credentials # type: ignore -from google.auth.transport.grpc import SslCredentials # type: ignore +from google.api_core import exceptions as core_exceptions # type: ignore +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore import grpc # type: ignore @@ -214,8 +215,10 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): {% endif %} ) - # Raise requests.exceptions.HTTPError if the status code is >= 400 - response.raise_for_status() + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) {% if not method.void %} # Return the response