Skip to content

feat(errors): register exception handlers by exception type #99

Description

@ZhuchkaTriplesix

Context

  • HTTPException maps to status + JSON body (oxyroute/exceptions.py, src/dispatch.rs).
  • Other exceptions → 500 with safe body (OXYROUTE_DEBUG for detail).
  • docs/feature.md notes missing register_exception_handler (partial scope of feat: global HTTPException and exception handlers #48).

Problem

Users cannot register handlers for domain-specific exception types (e.g. NotFoundError, ValidationError) without wrapping every route handler.

Proposed change

  • App.register_exception_handler(exc_type, handler) storing a type → callable registry.
  • On PyErr from route / dependency / middleware, walk the registry (subclass match) and map to Response / dict / str like normal handlers.
  • Production 500 remains safe when no handler matches.

Files

  • oxyroute/app.py, oxyroute/exceptions.py, src/dispatch.rs, src/lib.rs
  • docs/handlers.md, tests

Acceptance criteria

  • Custom exception maps to expected status and body.
  • Subclass matching is predictable (most specific wins).
  • Unhandled exceptions still return safe 500 JSON.
  • Docs include a minimal example.

Framework impact

Centralized error handling for domain and validation errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions