diff --git a/bigbuy/exceptions.py b/bigbuy/exceptions.py index 6fb5992..ffe0ad1 100644 --- a/bigbuy/exceptions.py +++ b/bigbuy/exceptions.py @@ -12,9 +12,8 @@ from datetime import datetime, timedelta from typing import Optional, Union, Dict, Any, List, Type, cast, Sequence -from requests import Response - from bigbuy.rate_limit import RateLimit +from requests import Response class BBError(Exception): @@ -370,6 +369,10 @@ def raise_for_response(response: Response): bb_code = content["code"] message = content["message"] + # {"code": "Bad request", "message": 400} + if isinstance(message, int) and 400 <= message <= 599 and isinstance(bb_code, str): + bb_code, message = str(message), bb_code + if "error_detail" in content and "different warehouses" in message: error_detail = content["error_detail"] if isinstance(error_detail, dict) and "warehouses" in error_detail: diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 50698cc..094db2d 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -147,6 +147,15 @@ def test_raise_for_response_soft_409(): with pytest.raises(ex.BBResponseError, match="Something went wrong"): ex.raise_for_response(response) +def test_raise_for_response_inverted_code_and_message(): + response = Response() + response.status_code = 400 + response.encoding = "utf-8" + payload = {'code': 'Bad request', 'message': 400} + response._content = json.dumps(payload).encode("utf-8") + + with pytest.raises(ex.BBResponseError, match="Bad request"): + ex.raise_for_response(response) def test_raise_for_response_soft_error_headers_in_body(): """