@@ -508,26 +508,32 @@ def _request(
508
508
method , url , headers = headers , params = params , json = body
509
509
)
510
510
_LOGGER .debug (
511
- f"Request response: { response .status_code } : { response .json () } "
511
+ f"Request response: { response .status_code } : { response .text } "
512
512
)
513
513
response .raise_for_status ()
514
514
return response .json ()
515
515
except HTTPError :
516
+ json_payload = {}
517
+ try :
518
+ json_payload = response .json ()
519
+ except json .decoder .JSONDecodeError :
520
+ _LOGGER .debug ("Invalid JSON payload received" )
521
+
516
522
if auth_request :
517
523
if (
518
524
response .status_code == 400
519
- and response . json ()[ "error" ] == "invalid_grant"
525
+ and json_payload . get ( "error" ) == "invalid_grant"
520
526
):
521
527
raise InvalidTokenError (
522
528
"ecobee tokens invalid; re-authentication required"
523
529
)
524
530
else :
525
531
_LOGGER .error (
526
532
f"Error requesting authorization from ecobee: "
527
- f"{ response .status_code } : { response . json () } "
533
+ f"{ response .status_code } : { json_payload } "
528
534
)
529
535
elif response .status_code == 500 :
530
- code = response . json ()[ "status" ][ "code" ]
536
+ code = json_payload . get ( "status" , {}). get ( "code" )
531
537
if code in [1 , 16 ]:
532
538
raise InvalidTokenError (
533
539
"ecobee tokens invalid; re-authentication required"
@@ -539,12 +545,12 @@ def _request(
539
545
else :
540
546
_LOGGER .error (
541
547
f"Error from ecobee while attempting to { log_msg_action } : "
542
- f"{ code } : { response . json ()[ 'status' ][ 'message' ] } "
548
+ f"{ code } : { json_payload . get ( 'status' , {}). get ( 'message' , 'Unknown error' ) } "
543
549
)
544
550
else :
545
551
_LOGGER .error (
546
552
f"Error from ecobee while attempting to { log_msg_action } : "
547
- f"{ response .status_code } : { response . json () } "
553
+ f"{ response .status_code } : { json_payload } "
548
554
)
549
555
except (RequestException , json .decoder .JSONDecodeError ):
550
556
_LOGGER .error (
0 commit comments