-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add error handling #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds centralized HTTP error handling to the CodeOcean client by defining a hierarchy of custom exception types and wiring a response hook to raise them based on status codes.
- Introduces a base
Error
dataclass and HTTP-specific subclasses (BadRequestError
,UnauthorizedError
, etc.) inerrors.py
. - Implements an
error_handler
inclient.py
that inspects response payloads and raises the corresponding custom exception. - Updates the session setup to use the new error hook.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/codeocean/errors.py | Defines Error base class and specific HTTP exception subclasses |
src/codeocean/client.py | Adds error_handler hook to map HTTP responses to custom exceptions |
Comments suppressed due to low confidence (2)
src/codeocean/errors.py:16
- Change the return type of
with_error
to-> T
instead of-> Error
so that chaining on subclasses preserves the specific exception type.
def with_error(self, ex: requests.HTTPError) -> Error:
src/codeocean/client.py:41
- Add unit tests for
error_handler
to verify that each HTTP status code correctly maps to the corresponding custom exception and carries the original error payload.
def error_handler(self, response, *args, **kwargs):
self.session.hooks["response"] = [self.error_handler] | ||
self.session.mount(self.domain, TCPKeepAliveAdapter(max_retries=self.retries)) | ||
|
||
self.capsules = Capsules(client=self.session) | ||
self.computations = Computations(client=self.session) | ||
self.data_assets = DataAssets(client=self.session) | ||
|
||
def error_handler(self, response, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider renaming this internal hook method to _error_handler
to signal it's for private use and avoid exposing it as part of the public API.
self.session.hooks["response"] = [self.error_handler] | |
self.session.mount(self.domain, TCPKeepAliveAdapter(max_retries=self.retries)) | |
self.capsules = Capsules(client=self.session) | |
self.computations = Computations(client=self.session) | |
self.data_assets = DataAssets(client=self.session) | |
def error_handler(self, response, *args, **kwargs): | |
self.session.hooks["response"] = [self._error_handler] | |
self.session.mount(self.domain, TCPKeepAliveAdapter(max_retries=self.retries)) | |
self.capsules = Capsules(client=self.session) | |
self.computations = Computations(client=self.session) | |
self.data_assets = DataAssets(client=self.session) | |
def _error_handler(self, response, *args, **kwargs): |
Copilot uses AI. Check for mistakes.
No description provided.