Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.27.7
-------
- Include the server's error message in the HTTPError raised by raise_for_status for all error statuses

1.27.6
-------
- Only parse the response body and decode the reason in raise_for_status when an error is actually raised
Expand Down
2 changes: 1 addition & 1 deletion intezer_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.27.6'
__version__ = '1.27.7'
6 changes: 5 additions & 1 deletion intezer_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def raise_for_status(response: requests.Response,
"""Raises stored :class:`HTTPError`, if one occurred."""
should_raise = False
http_error_msg = ''
response_json = {}
if statuses_to_ignore and response.status_code in statuses_to_ignore:
return
elif allowed_statuses and response.status_code not in allowed_statuses:
Expand All @@ -43,7 +44,7 @@ def raise_for_status(response: requests.Response,
try:
response_json = response.json()
except Exception:
response_json = {}
pass
should_raise = True
if response.status_code == HTTPStatus.UNAUTHORIZED:
raise errors.InvalidApiKeyError(response)
Expand All @@ -65,6 +66,9 @@ def raise_for_status(response: requests.Response,
else:
reason = response.reason
http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}'
server_error = response_json.get('error')
if server_error:
http_error_msg = f'{http_error_msg}, server returns {server_error}'
else:
http_error_msg = f'{http_error_msg}, server returns {response_json.get("error")}, details: {response_json.get("details")}'
raise requests.HTTPError(http_error_msg, response=response)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from http import HTTPStatus
from unittest.mock import MagicMock

import requests
import responses

from intezer_sdk import consts
Expand Down Expand Up @@ -84,6 +85,29 @@ def test_api_raise_insufficient_permissions_error_when_insufficient_permissions_
with self.assertRaises(errors.InsufficientPermissionsError):
raise_for_status(response)

def test_api_raises_http_error_with_server_error_message_when_conflict_received(self):
# Arrange
with responses.RequestsMock() as mock:
mock.add('POST',
url=f'{self.full_url}/get-access-token',
status=HTTPStatus.OK,
json={'result': 'access-token', 'expire_at': 2166920067})
api = set_global_api()
api.authenticate()

with responses.RequestsMock() as mock:
mock.add('POST',
f'{self.full_url}/some-route',
status=HTTPStatus.CONFLICT,
json={'error': 'Windows scanner version 1.0.1.20 is not supported'})
response = api.request_with_refresh_expired_access_token(method='POST', path='/some-route')

# Act & Assert
with self.assertRaises(requests.HTTPError) as context:
raise_for_status(response)

self.assertIn('Windows scanner version 1.0.1.20 is not supported', str(context.exception))

def test_api_raise_invalid_api_key_error_when_unauthorized_received(self):
# Arrange
with responses.RequestsMock() as mock:
Expand Down
Loading