Skip to content

Commit

Permalink
Allow error messages without code field (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk authored Jan 9, 2024
1 parent 32bb2a4 commit a9f254b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions alpaca_trade_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, error, http_error=None):

@property
def code(self):
return self._error['code']
return self._error.get('code', self.status_code)

@property
def status_code(self):
Expand All @@ -74,6 +74,15 @@ def response(self):
if self._http_error is not None:
return self._http_error.response

def raise_api_error(resp: requests.Response, http_error: requests.HTTPError):
try:
error = resp.json()
except:
raise http_error from None
if 'message' in error:
raise APIError(error, http_error) from None
raise http_error from None


class TimeFrameUnit(Enum):
Minute = "Min"
Expand Down Expand Up @@ -236,12 +245,7 @@ def _one_request(self, method: str, url: URL, opts: dict, retry: int):
# retry if we hit Rate Limit
if resp.status_code in retry_codes and retry > 0:
raise RetryException()
if 'code' in resp.text:
error = resp.json()
if 'code' in error:
raise APIError(error, http_error)
else:
raise
raise_api_error(resp, http_error)
if resp.text != '':
return resp.json()
return None
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ requests-mock
coverage>=4.4.1
mock>=1.0.1
flake8
deprecated
deprecated

0 comments on commit a9f254b

Please sign in to comment.