Skip to content

Commit

Permalink
refactor: simplify internal error handling (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Dec 13, 2023
1 parent d6221bf commit fcd454f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 222 deletions.
102 changes: 46 additions & 56 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,66 +873,61 @@ def _request(
request = self._build_request(options)
self._prepare_request(request)

response = None

try:
response = self._client.send(
request,
auth=self.custom_auth,
stream=stream or self._should_stream_response_body(request=request),
)
log.debug(
'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
)
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
if retries > 0 and self._should_retry(err.response):
err.response.close()
except httpx.TimeoutException as err:
if retries > 0:
return self._retry_request(
options,
cast_to,
retries,
err.response.headers,
stream=stream,
stream_cls=stream_cls,
response_headers=None,
)

# If the response is streamed then we need to explicitly read the response
# to completion before attempting to access the response text.
if not err.response.is_closed:
err.response.read()

raise self._make_status_error_from_response(err.response) from None
except httpx.TimeoutException as err:
if response is not None:
response.close()

raise APITimeoutError(request=request) from err
except Exception as err:
if retries > 0:
return self._retry_request(
options,
cast_to,
retries,
stream=stream,
stream_cls=stream_cls,
response_headers=response.headers if response is not None else None,
response_headers=None,
)

raise APITimeoutError(request=request) from err
except Exception as err:
if response is not None:
response.close()
raise APIConnectionError(request=request) from err

if retries > 0:
log.debug(
'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
)

try:
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
if retries > 0 and self._should_retry(err.response):
err.response.close()
return self._retry_request(
options,
cast_to,
retries,
err.response.headers,
stream=stream,
stream_cls=stream_cls,
response_headers=response.headers if response is not None else None,
)

raise APIConnectionError(request=request) from err
# If the response is streamed then we need to explicitly read the response
# to completion before attempting to access the response text.
if not err.response.is_closed:
err.response.read()

raise self._make_status_error_from_response(err.response) from None

return self._process_response(
cast_to=cast_to,
Expand Down Expand Up @@ -1340,66 +1335,61 @@ async def _request(
request = self._build_request(options)
await self._prepare_request(request)

response = None

try:
response = await self._client.send(
request,
auth=self.custom_auth,
stream=stream or self._should_stream_response_body(request=request),
)
log.debug(
'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
)
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
if retries > 0 and self._should_retry(err.response):
await err.response.aclose()
except httpx.TimeoutException as err:
if retries > 0:
return await self._retry_request(
options,
cast_to,
retries,
err.response.headers,
stream=stream,
stream_cls=stream_cls,
response_headers=None,
)

# If the response is streamed then we need to explicitly read the response
# to completion before attempting to access the response text.
if not err.response.is_closed:
await err.response.aread()

raise self._make_status_error_from_response(err.response) from None
except httpx.TimeoutException as err:
if response is not None:
await response.aclose()

raise APITimeoutError(request=request) from err
except Exception as err:
if retries > 0:
return await self._retry_request(
options,
cast_to,
retries,
stream=stream,
stream_cls=stream_cls,
response_headers=response.headers if response is not None else None,
response_headers=None,
)

raise APITimeoutError(request=request) from err
except Exception as err:
if response is not None:
await response.aclose()
raise APIConnectionError(request=request) from err

if retries > 0:
log.debug(
'HTTP Request: %s %s "%i %s"', request.method, request.url, response.status_code, response.reason_phrase
)

try:
response.raise_for_status()
except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code
if retries > 0 and self._should_retry(err.response):
await err.response.aclose()
return await self._retry_request(
options,
cast_to,
retries,
err.response.headers,
stream=stream,
stream_cls=stream_cls,
response_headers=response.headers if response is not None else None,
)

raise APIConnectionError(request=request) from err
# If the response is streamed then we need to explicitly read the response
# to completion before attempting to access the response text.
if not err.response.is_closed:
await err.response.aread()

raise self._make_status_error_from_response(err.response) from None

return self._process_response(
cast_to=cast_to,
Expand Down
Loading

0 comments on commit fcd454f

Please sign in to comment.