Skip to content

implement custom exceptions for Celeste and add unit tests#23

Merged
Kamilbenkirane merged 8 commits intowithceleste:mainfrom
abirzishan32:abir
Nov 9, 2025
Merged

implement custom exceptions for Celeste and add unit tests#23
Kamilbenkirane merged 8 commits intowithceleste:mainfrom
abirzishan32:abir

Conversation

@abirzishan32
Copy link
Copy Markdown
Contributor

@abirzishan32 abirzishan32 commented Nov 8, 2025

Improved Exception Handling and Type Safety

Summary

This PR enhances the Celeste library's error handling with a comprehensive custom exception hierarchy and adds robust input validation across core modules.

Changes Made

1. New Exception Classes (exceptions.py)

Added domain-specific exceptions for better error handling:

  • ValidationError - For parameter and constraint validation failures
  • ConfigurationError - For invalid configuration or setup issues
  • NetworkError - For HTTP/network-related failures
  • ProviderError - For provider-specific API errors
  • RateLimitError - For rate limiting and quota issues
  • ModelError - For model-related errors

All exceptions include:

  • Clear error messages with context
  • Optional details dict for debugging
  • Proper exception chaining with from clause

2. Updated Modules with Custom Exceptions

constraints.py

  • Replaced generic ValueError/TypeError with ValidationError
  • Better error messages with actual vs expected values

parameters.py

  • Uses ValidationError for constraint violations
  • Improved error context in validation failures

models.py

  • ConfigurationError for display name mismatches
  • ValidationError for capability/constraint issues

credentials.py

  • More descriptive MissingCredentialsError messages
  • Better error context for missing API keys

client.py

  • NetworkError for HTTP failures
  • ProviderError for API errors with response details
  • ConfigurationError for client setup issues

http.py

  • NetworkError for connection/timeout failures
  • Proper error chaining from httpx exceptions

3. Input Validation

Added validation to prevent runtime errors:

  • parameters.py: Validates mapper name is StrEnum
  • models.py: Validates model registration inputs
  • http.py: Validates timeout and connection limits

4. Comprehensive Tests

New test file tests/unit_tests/test_exceptions_enhanced.py with:

  • Exception hierarchy verification
  • Serialization/deserialization tests
  • Details dict handling
  • Exception chaining validation
  • Coverage for all new exception types

Benefits

Better Developer Experience: Clear, actionable error messages
Easier Debugging: Detailed context in exception details
Type Safety: All exceptions properly typed and documented
Backward Compatible: Extends existing exceptions without breaking changes
Testable: Custom exceptions can be caught and asserted specifically
Production Ready: Proper logging and error tracking support

abirzishan32 and others added 8 commits November 8, 2025 13:10
- Update test_init.py to expect ModelNotFoundError instead of ValueError
- Update test_credentials.py to expect MissingCredentialsError instead of ValueError
- Update test_client.py to expect ClientNotFoundError instead of NotImplementedError
- Update test_exceptions.py to use kebab-case capability strings (text-generation, image-generation, etc.)
Following the pattern of Client (not CelesteClient) and Parameter (not CelesteParameter),
the base exception is now Error (not CelesteError) for consistency.
Move exception imports from inside test functions to top of file
for proper Python import ordering.
…xports

- Replace all ValueError/TypeError in constraints.py with ConstraintViolationError
- Remove base exception classes from __all__ exports (keep for inheritance)
- Update all constraint tests to expect ConstraintViolationError
- Add ConstraintViolationError tests to test_exceptions.py

This makes constraint violations catchable as a specific exception type
while keeping the API surface minimal by not exporting base classes.
…rings

- Make ModelNotFoundError support optional parameters (model_id, provider, capability)
- Update _resolve_model to use ModelNotFoundError instead of ValueError for no models available
- Add UnsupportedCapabilityError to create_client docstring Raises section
- Update Client.stream() docstring to mention StreamingNotSupportedError
- Update tests to expect correct custom exceptions instead of generic ones
- Remove .value attribute access from StrEnum instances (Capability, Provider, Parameter)
- StrEnum instances can be used directly as strings
- Updated all exception calls to pass StrEnum directly instead of .value
@Kamilbenkirane Kamilbenkirane merged commit a500a2f into withceleste:main Nov 9, 2025
8 of 9 checks passed
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.

2 participants