Skip to content

Commit

Permalink
[SEA-1683] Fix retry mechanism for 400 errors (#53)
Browse files Browse the repository at this point in the history
We were unnecessarily retrying on all 400 errors except 401. This change
should fix the issue.
  • Loading branch information
corafid authored Dec 2, 2024
1 parent 4f6fadd commit 1c22d5c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
33 changes: 10 additions & 23 deletions compass_sdk/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
DEFAULT_MAX_RETRIES,
DEFAULT_SLEEP_RETRY_SECONDS,
)
from compass_sdk.exceptions import CompassAuthError, CompassClientError, CompassMaxErrorRateExceeded
from compass_sdk.models import (
CreateDataSource,
DataSource,
Expand All @@ -50,28 +51,6 @@ class RetryResult:
error: Optional[str] = None


class CompassAuthError(Exception):
"""Exception raised for authentication errors in the Compass client."""

def __init__(
self,
message=("CompassAuthError - check your bearer token or username and password."),
):
self.message = message
super().__init__(self.message)


class CompassMaxErrorRateExceeded(Exception):
"""Exception raised when the error rate exceeds the maximum allowed error rate in the Compass client."""

def __init__(
self,
message="The maximum error rate was exceeded. Stopping the insertion process.",
):
self.message = message
super().__init__(self.message)


_DEFAULT_TIMEOUT = 30


Expand Down Expand Up @@ -636,7 +615,12 @@ def _send_request(
@retry(
stop=stop_after_attempt(max_retries),
wait=wait_fixed(sleep_retry_seconds),
retry=retry_if_not_exception_type((CompassAuthError, InvalidSchema)),
retry=retry_if_not_exception_type(
(
CompassClientError,
InvalidSchema,
)
),
)
def _send_request_with_retry():
nonlocal error
Expand Down Expand Up @@ -667,6 +651,9 @@ def _send_request_with_retry():
if e.response.status_code == 401:
error = "Unauthorized. Please check your username and password."
raise CompassAuthError(message=str(e))
elif 400 <= e.response.status_code < 500:
error = f"Client error occurred: {e.response.text}"
raise CompassClientError(message=error)
else:
error = str(e) + " " + e.response.text
logger.error(
Expand Down
29 changes: 29 additions & 0 deletions compass_sdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class CompassClientError(Exception):
"""Exception raised for all 4xx client errors in the Compass client."""

def __init__(self, message="Client error occurred."):
self.message = message
super().__init__(self.message)


class CompassAuthError(CompassClientError):
"""Exception raised for authentication errors in the Compass client."""

def __init__(
self,
message=("CompassAuthError - check your bearer token or username and password."),
):
self.message = message
super().__init__(self.message)


class CompassMaxErrorRateExceeded(Exception):
"""Exception raised when the error rate exceeds the maximum allowed error rate in
the Compass client."""

def __init__(
self,
message="The maximum error rate was exceeded. Stopping the insertion process.",
):
self.message = message
super().__init__(self.message)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "compass-sdk"
version = "0.5.0"
version = "0.5.1"
authors = []
description = "Compass SDK"

Expand Down

0 comments on commit 1c22d5c

Please sign in to comment.