@@ -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+
525545def exception_class_for_grpc_status (status_code ):
526546 """Return the exception class for a specific :class:`grpc.StatusCode`.
527547
0 commit comments