Skip to content

Commit

Permalink
fix(api.py): remove redundant error handling for non-success response…
Browse files Browse the repository at this point in the history
… codes

feat(api.py): add handling for HTTPStatusError to improve error reporting
refactor(api.py): use response object methods for error handling to enhance code clarity
fix(exceptions.py): update OtfRequestError to use httpx Response and Request types for better type safety
  • Loading branch information
NodeJSmith committed Jan 7, 2025
1 parent bf6d55c commit 23ffa0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
16 changes: 7 additions & 9 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ def _do(
LOGGER.exception(f"Error making request: {e}")
LOGGER.exception(f"Response: {response.text}")
raise
except httpx.HTTPStatusError as e:
LOGGER.exception(f"Error making request: {e}")
raise exc.OtfRequestError("Error making request", response=response, request=request)
except Exception as e:
LOGGER.exception(f"Error making request: {e}")
raise

if isinstance(response, dict) and "code" in response and response["code"] != "SUCCESS":
LOGGER.error(f"Error making request: {response}")
LOGGER.error(f"Response: {response.text}")
raise exc.OtfRequestError("Error making request", response=response, request=request)

return response.json()

def _classes_request(self, method: str, url: str, params: dict[str, Any] | None = None) -> Any:
Expand Down Expand Up @@ -272,12 +270,12 @@ def book_class(self, otf_class: str | models.OtfClass) -> models.Booking:
try:
resp = self._default_request("PUT", f"/member/members/{self.member_uuid}/bookings", json=body)
except exc.OtfRequestError as e:
resp = e.response
resp_obj = e.response.json()

if resp["code"] == "ERROR":
if resp["data"]["errorCode"] == "603":
if resp_obj["code"] == "ERROR":
if resp_obj["data"]["errorCode"] == "603":
raise exc.AlreadyBookedError(f"Class {class_uuid} is already booked.")
if resp["data"]["errorCode"] == "602":
if resp_obj["data"]["errorCode"] == "602":
raise exc.OutsideSchedulingWindowError(f"Class {class_uuid} is outside the scheduling window.")

raise
Expand Down
9 changes: 6 additions & 3 deletions src/otf_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from httpx import Request, Response


class OtfException(Exception):
"""Base class for all exceptions in this package."""


class OtfRequestError(OtfException):
"""Raised when an error occurs while making a request to the OTF API."""

response: dict
request: dict
response: Response
request: Request

def __init__(self, message: str, response: dict, request: dict):
def __init__(self, message: str, response: Response, request: Request):
super().__init__(message)
self.response = response
self.request = request
Expand Down

0 comments on commit 23ffa0d

Please sign in to comment.