Skip to content

Commit

Permalink
support another strange BigBuy error format
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Apr 16, 2024
1 parent ef9514a commit 167559f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bigbuy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down

0 comments on commit 167559f

Please sign in to comment.