From 32dfe30a940b116cda6927d595c041a7dd1ec54e Mon Sep 17 00:00:00 2001 From: Roman Kalyakin Date: Thu, 24 Oct 2024 14:07:53 +0200 Subject: [PATCH] handle known firewall request rejected errors --- impresso/client.py | 4 ++++ impresso/util/error.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/impresso/client.py b/impresso/client.py index 5fa0b9c..de470b1 100644 --- a/impresso/client.py +++ b/impresso/client.py @@ -11,6 +11,7 @@ from impresso.api_client import AuthenticatedClient from impresso.client_base import ImpressoApiResourcesBase from impresso.config_file import DEFAULT_API_URL, ImpressoPyConfig +from impresso.util.error import handle_known_errors from impresso.util.token import get_jwt_status logger = logging.getLogger(__name__) @@ -28,6 +29,9 @@ def _is_localhost_netloc(netloc: str) -> bool: def _log_non_2xx(response: httpx.Response) -> None: if response.status_code >= 400: response.read() + + handle_known_errors(response.status_code, response.text) + logging.error( f"Received error response ({response.status_code}): {response.text}" ) diff --git a/impresso/util/error.py b/impresso/util/error.py index e55c47e..ca23243 100644 --- a/impresso/util/error.py +++ b/impresso/util/error.py @@ -1,7 +1,9 @@ +from json import JSONDecodeError from typing import TypeVar from impresso.api_client.models.error import Error as ApiError from impresso.api_models import Error +import re IT = TypeVar("IT") @@ -25,3 +27,13 @@ def raise_for_error(result: ApiError | IT) -> IT: raise ImpressoError(error) else: return result + + +def handle_known_errors(status_code: int, response_text: str) -> None: + # Known firewall errors + match = re.search(r"Your support ID is: ([^\s<>]+)", response_text) + if match: + support_id = match.group(1) + raise ValueError( + f"Request rejected. Please contact Impresso team on info@impresso-project.ch quoting the ID: {support_id}." + )