Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/anthropic/lib/bedrock/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def _make_status_error(
if response.status_code == 409:
return _exceptions.ConflictError(err_msg, response=response, body=body)

if response.status_code == 413:
return _exceptions.RequestTooLargeError(err_msg, response=response, body=body)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)

Expand All @@ -123,6 +126,9 @@ def _make_status_error(
if response.status_code == 503:
return _exceptions.ServiceUnavailableError(err_msg, response=response, body=body)

if response.status_code == 529:
return _exceptions.OverloadedError(err_msg, response=response, body=body)

if response.status_code >= 500:
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)
Expand Down
6 changes: 6 additions & 0 deletions src/anthropic/lib/vertex/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def _make_status_error(
if response.status_code == 409:
return _exceptions.ConflictError(err_msg, response=response, body=body)

if response.status_code == 413:
return _exceptions.RequestTooLargeError(err_msg, response=response, body=body)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)

Expand All @@ -82,6 +85,9 @@ def _make_status_error(
if response.status_code == 504:
return _exceptions.DeadlineExceededError(err_msg, response=response, body=body)

if response.status_code == 529:
return _exceptions.OverloadedError(err_msg, response=response, body=body)

if response.status_code >= 500:
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
from respx import MockRouter

from anthropic import AnthropicBedrock, AsyncAnthropicBedrock
from anthropic._exceptions import (
BadRequestError,
AuthenticationError,
PermissionDeniedError,
NotFoundError,
ConflictError,
RequestTooLargeError,
UnprocessableEntityError,
RateLimitError,
ServiceUnavailableError,
OverloadedError,
InternalServerError,
)

sync_client = AnthropicBedrock(
aws_region="us-east-1",
Expand Down Expand Up @@ -195,3 +208,25 @@ def test_region_infer_from_specified_profile(
client = AnthropicBedrock()

assert client.aws_region == next(profile for profile in profiles if profile["name"] == aws_profile)["region"]


@pytest.mark.parametrize(
"status_code, expected_error",
[
(400, BadRequestError),
(401, AuthenticationError),
(403, PermissionDeniedError),
(404, NotFoundError),
(409, ConflictError),
(413, RequestTooLargeError),
(422, UnprocessableEntityError),
(429, RateLimitError),
(500, InternalServerError),
(503, ServiceUnavailableError),
(529, OverloadedError),
],
)
def test_make_status_error(status_code: int, expected_error: type) -> None:
response = httpx.Response(status_code, request=httpx.Request("GET", "/"))
err = sync_client._make_status_error("msg", body=None, response=response)
assert type(err) is expected_error
40 changes: 40 additions & 0 deletions tests/lib/test_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
from respx import MockRouter

from anthropic import AnthropicVertex, AsyncAnthropicVertex
from anthropic._exceptions import (
BadRequestError,
AuthenticationError,
PermissionDeniedError,
NotFoundError,
ConflictError,
RequestTooLargeError,
UnprocessableEntityError,
RateLimitError,
ServiceUnavailableError,
DeadlineExceededError,
OverloadedError,
InternalServerError,
)

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")

Expand Down Expand Up @@ -276,3 +290,29 @@ def test_env_var_base_url_override(self, monkeypatch: pytest.MonkeyPatch) -> Non
base_url="https://test.googleapis.com/v1",
)
assert str(client.base_url).rstrip("/") == "https://test.googleapis.com/v1"


vertex_client = AnthropicVertex(region="region", project_id="project", access_token="my-access-token")


@pytest.mark.parametrize(
"status_code, expected_error",
[
(400, BadRequestError),
(401, AuthenticationError),
(403, PermissionDeniedError),
(404, NotFoundError),
(409, ConflictError),
(413, RequestTooLargeError),
(422, UnprocessableEntityError),
(429, RateLimitError),
(500, InternalServerError),
(503, ServiceUnavailableError),
(504, DeadlineExceededError),
(529, OverloadedError),
],
)
def test_make_status_error(status_code: int, expected_error: type) -> None:
response = httpx.Response(status_code, request=httpx.Request("GET", "/"))
err = vertex_client._make_status_error("msg", body=None, response=response)
assert type(err) is expected_error