Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: add error handling #47

wants to merge 1 commit into from

Conversation

jake-valsamis
Copy link
Contributor

No description provided.

Copy link

@Copilot Copilot AI left a 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.) in errors.py.
  • Implements an error_handler in client.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):

Comment on lines +34 to +41
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):
Copy link
Preview

Copilot AI Jun 13, 2025

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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant