@@ -475,23 +475,30 @@ def from_http_status(status_code, message, **kwargs):
475
475
476
476
return error
477
477
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
478
486
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 .
481
489
482
490
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.
484
495
485
496
Returns:
486
497
GoogleAPICallError: An instance of the appropriate subclass of
487
498
:class:`GoogleAPICallError`, with the message and errors populated
488
499
from the response.
489
500
"""
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
495
502
error_message = payload .get ("error" , {}).get ("message" , "unknown error" )
496
503
errors = payload .get ("error" , {}).get ("errors" , ())
497
504
# In JSON, details are already formatted in developer-friendly way.
@@ -504,12 +511,7 @@ def from_http_response(response):
504
511
)
505
512
)
506
513
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 )
513
515
514
516
exception = from_http_status (
515
517
response .status_code ,
@@ -522,6 +524,24 @@ def from_http_response(response):
522
524
return exception
523
525
524
526
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
+
525
545
def exception_class_for_grpc_status (status_code ):
526
546
"""Return the exception class for a specific :class:`grpc.StatusCode`.
527
547
0 commit comments