Skip to content

Commit

Permalink
refactor(api.py): simplify exception handling by passing original exc…
Browse files Browse the repository at this point in the history
…eption to OtfRequestError

feat(exceptions.py): add original_exception attribute to OtfRequestError for better error context
  • Loading branch information
NodeJSmith committed Jan 22, 2025
1 parent 7da1d58 commit e3cb63a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ def _do(
LOGGER.exception(f"Response: {response.text}")
raise
except httpx.HTTPStatusError as e:
LOGGER.exception(f"Error making request: {e}")
LOGGER.exception(f"Response: {response.text}")
raise exc.OtfRequestError("Error making request", response=response, request=request)
raise exc.OtfRequestError("Error making request", e, response=response, request=request)
except Exception as e:
LOGGER.exception(f"Error making request: {e}")
raise
Expand All @@ -110,7 +108,7 @@ def _do(
and not (resp["Status"] >= 200 and resp["Status"] <= 299)
):
LOGGER.error(f"Error making request: {resp}")
raise exc.OtfRequestError("Error making request", response=response, request=request)
raise exc.OtfRequestError("Error making request", None, response=response, request=request)

return resp

Expand Down Expand Up @@ -966,6 +964,7 @@ def get_performance_summary(self, performance_summary_id: str) -> models.Perform

path = f"/v1/performance-summaries/{performance_summary_id}"
res = self._performance_summary_request("GET", path)

if res is None:
raise exc.ResourceNotFoundError(f"Performance summary {performance_summary_id} not found")

Expand Down
4 changes: 3 additions & 1 deletion src/otf_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ class OtfException(Exception):
class OtfRequestError(OtfException):
"""Raised when an error occurs while making a request to the OTF API."""

original_exception: Exception
response: Response
request: Request

def __init__(self, message: str, response: Response, request: Request):
def __init__(self, message: str, original_exception: Exception | None, response: Response, request: Request):
super().__init__(message)
self.original_exception = original_exception
self.response = response
self.request = request

Expand Down

0 comments on commit e3cb63a

Please sign in to comment.