Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for handling conflict error TKT-4518 #144

Merged
merged 2 commits into from
Jul 16, 2024
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.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
Loading