Skip to content

Handling 503 Response Code #32

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

Merged
merged 1 commit into from
Apr 9, 2021
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
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ as a `ScaleException` parent type and child exceptions:
- ``ScaleResourceNotFound``: 404 - Not Found -- The requested resource doesn't exist.
- ``ScaleDuplicateTask``: 409 - Conflict -- The provided idempotency key or unique_id is already in use for a different request.
- ``ScaleTooManyRequests``: 429 - Too Many Requests -- Too many requests hit the API too quickly.
- ``ScaleInternalError``: 500 - Internal Server Error -- We had a problem with our server. Try again later
- ``ScaleInternalError``: 500 - Internal Server Error -- We had a problem with our server. Try again later.
- ``ScaleServiceUnavailable``: 503 - Server Timeout From Request Queueing -- Try again later.
- ``ScaleTimeoutError``: 504 - Server Timeout Error -- Try again later.

Check out `Scale's API documentation <https://docs.scale.com/reference#errors>`_ for more details.
Expand Down
2 changes: 1 addition & 1 deletion scaleapi/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.0.2"
__version__ = "2.0.3"
__package_name__ = "scaleapi"
5 changes: 2 additions & 3 deletions scaleapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Parameters for HTTP retry
HTTP_TOTAL_RETRIES = 3 # Number of total retries
HTTP_RETRY_BACKOFF_FACTOR = 2 # Wait 1, 2, 4 seconds between retries
HTTP_STATUS_FORCE_LIST = [429, 500, 504] # Status codes to force retry
HTTP_STATUS_FORCE_LIST = [429, 500, 503, 504] # Status codes to force retry
HTTP_RETRY_ALLOWED_METHODS = frozenset({"GET", "POST"})


Expand Down Expand Up @@ -68,7 +68,6 @@ def _http_request(
@staticmethod
def _raise_on_respose(res: Response):

message = ""
try:
message = res.json().get("error", res.text)
except ValueError:
Expand All @@ -78,7 +77,7 @@ def _raise_on_respose(res: Response):
exception = ExceptionMap[res.status_code]
raise exception(message)
except KeyError as err:
raise ScaleException(message) from err
raise ScaleException(message, res.status_code) from err

def _api_request(
self, method, endpoint, headers=None, auth=None, params=None, body=None
Expand Down
7 changes: 7 additions & 0 deletions scaleapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class ScaleInternalError(ScaleException):
code = 500


class ScaleServiceUnavailable(ScaleException):
"""503 - Server Timeout From Request Queueing -- Try again later."""

code = 503


class ScaleTimeoutError(ScaleException):
"""504 - Server Timeout Error -- Try again later."""

Expand All @@ -84,4 +90,5 @@ class ScaleTimeoutError(ScaleException):
ScaleTooManyRequests.code: ScaleTooManyRequests,
ScaleInternalError.code: ScaleInternalError,
ScaleTimeoutError.code: ScaleTimeoutError,
ScaleServiceUnavailable.code: ScaleServiceUnavailable,
}