Skip to content

Commit

Permalink
handle known firewall request rejected errors
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm committed Oct 24, 2024
1 parent de90640 commit 32dfe30
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions impresso/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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}"
)
Expand Down
12 changes: 12 additions & 0 deletions impresso/util/error.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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 [email protected] quoting the ID: {support_id}."
)

0 comments on commit 32dfe30

Please sign in to comment.