Skip to content

Commit

Permalink
Merge pull request #144 from intezer/fix/response-error-message
Browse files Browse the repository at this point in the history
fix for handling conflict error TKT-4518
  • Loading branch information
ofirit authored Jul 16, 2024
2 parents 624e7f0 + fdfcfc7 commit d14bd3b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.21.3
_______
- Refactor: simplified raise for status

1.21.2
_______
- Fix handling of large offline endpoint scan info files
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.21.2'
__version__ = '1.21.3'
49 changes: 18 additions & 31 deletions intezer_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import logging
import os
import typing
from http import HTTPStatus
from typing import Any
from typing import BinaryIO
Expand All @@ -25,12 +24,16 @@

logger = logging.getLogger(__name__)


def raise_for_status(response: requests.Response,
statuses_to_ignore: List[Union[HTTPStatus, int]] = None,
allowed_statuses: List[Union[HTTPStatus, int]] = None):
"""Raises stored :class:`HTTPError`, if one occurred."""
try:
response_json = response.json()
except Exception:
response_json = {}

should_raise = False
http_error_msg = ''
if isinstance(response.reason, bytes):
reason = response.reason.decode('utf-8', 'ignore')
Expand All @@ -40,42 +43,26 @@ def raise_for_status(response: requests.Response,
if statuses_to_ignore and response.status_code in statuses_to_ignore:
return
elif allowed_statuses and response.status_code not in allowed_statuses:
http_error_msg = f'{response.status_code} Custom Error: {reason} for url: {response.url}'
elif 400 <= response.status_code < 500:
should_raise = True
elif 400 <= response.status_code < 600:
should_raise = True
if response.status_code == HTTPStatus.UNAUTHORIZED:
raise errors.InvalidApiKeyError(response)
elif response.status_code == HTTPStatus.CONFLICT:
is_skipped_by_rule = response.json().get('result', {}).get('is_skipped_by_rule')
if is_skipped_by_rule:
if response_json.get('result', {}).get('is_skipped_by_rule'):
raise errors.AnalysisSkippedByRuleError(response)
elif response.status_code == HTTPStatus.FORBIDDEN:
try:
error_message = response.json()['error']
except Exception:
http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}'
else:
if error_message == 'Insufficient Permissions':
raise errors.InsufficientPermissionsError(response)
elif response.status_code != HTTPStatus.BAD_REQUEST:
if response_json.get('error') == 'Insufficient Permissions':
raise errors.InsufficientPermissionsError(response)
elif response.status_code == HTTPStatus.BAD_REQUEST:
http_error_msg = '\n'.join([f'{key}:{value}.' for key, value in response_json.get('message', {}).items()])


if should_raise:
if not http_error_msg:
http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}'
else:
# noinspection PyBroadException
try:
error = response.json()
http_error_msg = '\n'.join([f'{key}:{value}.' for key, value in error['message'].items()])
except Exception:
http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}'
elif 500 <= response.status_code < 600:
http_error_msg = f'{response.status_code} Server Error: {reason} for url: {response.url}'

if http_error_msg:
# noinspection PyBroadException
try:
data = response.json()
http_error_msg = f'{http_error_msg}, server returns {data["error"]}, details: {data.get("details")}'
except Exception:
pass

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

0 comments on commit d14bd3b

Please sign in to comment.