Skip to content

Commit

Permalink
Client: Separate ClientError and ServerError so retries work correctl…
Browse files Browse the repository at this point in the history
…y (#12645)

GitOrigin-RevId: 61d7c72d361df2a6bd182e42ee3954481c31fd5f
  • Loading branch information
stephencpope authored and Descartes Labs Build committed Aug 17, 2024
1 parent 411653d commit 95d216a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions descarteslabs/core/common/http/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def SOCKSProxyManager(*args, **kwargs):

from descarteslabs.exceptions import (
BadRequestError,
ClientError,
ConflictError,
ForbiddenError,
GatewayTimeoutError,
GoneError,
MethodNotAllowedError,
NotFoundError,
ProxyAuthenticationRequiredError,
RateLimitError,
RequestCancellationError,
ServerError,
UnauthorizedError,
ValidationError,
)

Expand Down Expand Up @@ -396,11 +400,17 @@ def request(self, method, url, headers=None, **kwargs):
return resp
elif resp.status_code == HTTPStatus.BAD_REQUEST:
raise BadRequestError(resp.text)
elif resp.status_code == HTTPStatus.UNAUTHORIZED:
raise UnauthorizedError(resp.text)
elif resp.status_code == HTTPStatus.FORBIDDEN:
raise ForbiddenError(resp.text)
elif resp.status_code == HTTPStatus.NOT_FOUND:
text = resp.text
if not text:
text = "{} {} {}".format(HTTPStatus.NOT_FOUND, method, url)
raise NotFoundError(text)
elif resp.status_code == HTTPStatus.METHOD_NOT_ALLOWED:
raise MethodNotAllowedError(resp.text)
elif resp.status_code == HTTPStatus.PROXY_AUTHENTICATION_REQUIRED:
raise ProxyAuthenticationRequiredError(
resp.text,
Expand All @@ -417,6 +427,10 @@ def request(self, method, url, headers=None, **kwargs):
raise RateLimitError(
resp.text, retry_after=resp.headers.get(HttpHeaderKeys.RetryAfter)
)
elif resp.status_code < HTTPStatus.INTERNAL_SERVER_ERROR:
ex = ClientError(resp.text)
ex.status = resp.status_code.value
raise ex
elif resp.status_code == HTTPStatus.GATEWAY_TIMEOUT:
raise GatewayTimeoutError(
"Your request timed out on the server. "
Expand Down
24 changes: 21 additions & 3 deletions descarteslabs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ class BadRequestError(ClientError):
status = 400


class ValidationError(BadRequestError):
"""Client request with invalid parameters."""
class UnauthorizedError(ClientError):
"""Client request lacking authentication."""

status = 401

status = 422

class ForbiddenError(ClientError):
"""Client request lacks necessary permissions."""

status = 403


class NotFoundError(ClientError):
Expand All @@ -64,6 +70,12 @@ class NotFoundError(ClientError):
status = 404


class MethodNotAllowedError(ClientError):
"""Requested nethod not supported by the resource."""

status = 405


class ProxyAuthenticationRequiredError(ClientError):
"""Client request needs proxy authentication.
Expand Down Expand Up @@ -96,6 +108,12 @@ class GoneError(ClientError):
status = 410


class ValidationError(BadRequestError):
"""Client request with invalid parameters."""

status = 422


class RateLimitError(ClientError):
"""
Client request exceeds rate limits.
Expand Down

0 comments on commit 95d216a

Please sign in to comment.