feat(errors): typed 4xx subclasses + validation envelope handler (closes #32)#47
Merged
Conversation
#32) Add ergonomic ``Hal0Error`` subclasses for the everyday HTTP client-error statuses — ``BadRequest`` (400), ``Unauthorized`` (401), ``Forbidden`` (403), ``NotFound`` (404), ``Conflict`` (409), ``UnprocessableEntity`` (422). Each carries a sensible default ``code`` and accepts a per-instance ``code=`` override so route sites get the right status + envelope without sub-subclassing for every distinct error. Register a ``RequestValidationError`` exception handler that reshapes FastAPI's default ``{"detail": [...]}`` 422 into the canonical hal0 envelope ``{"error": {"code, message, details}}`` with ``code="validation.invalid"``, downgrading the status to 400 (caller-side input errors, not "well-formed but semantically rejected"). Touches two sanity-test call sites only (auth + slots body validation); the wave-2 migration of every existing bare ``Hal0Error("...")`` raise is intentionally out of scope (issues #34/#35/#36/#37 will rebase on this). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
thinmintdev
added a commit
that referenced
this pull request
May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #32.
Hal0Error4xx subclasses (BadRequest400,Unauthorized401,Forbidden403,NotFound404,Conflict409,UnprocessableEntity422) inhal0.errors, each with a sensible defaultcodeand an optional per-instancecode=override so routes can pick a stable namespaced code per error site without sub-subclassing.RequestValidationErrorhandler inhal0.api.middleware.error_codesthat reshapes FastAPI's default{"detail": [...]}422 into the canonical envelope{"error": {"code, message, details: {errors: [...]}}}withcode="validation.invalid", downgrading the status to 400 (caller-side input errors, not "well-formed but semantically rejected" — that meaning is reserved for explicitUnprocessableEntityraises).hal0.api.middleware.error_codesso routes can use the import path most of them already use.Scope
This is the foundation for the wave-2 cleanup (#34/#35/#36/#37) — those issues should rebase on this branch. The PR only refactors two sanity-test call sites (auth + slots body validation) to prove the round-trip; bulk migration of every existing
Hal0Error("...")raise is intentionally out of scope here.The
/v1/*(OpenAI-shaped) error path is untouched — those routes already use typedDispatchErrorsubclasses and share the same middleware envelope today.What's new
hal0.errors.BadRequesthal0.errors.Unauthorizedhal0.errors.Forbiddenhal0.errors.NotFoundhal0.errors.Conflicthal0.errors.UnprocessableEntityAll re-exported from
hal0.api.middleware.error_codes.Handler registration (lives inside
install(app)inerror_codes.py):Test plan
tests/api/test_typed_errors.pyparametrizes all six subclasses against status + envelope round-trip, pluscode=override,detailsdefaulting, and the legacy sub-subclass pattern.UnprocessableEntitykeeps its 422 (distinct from the auto-downgraded pydantic path).tests/api/test_logs_routes.pyupdated for the new envelope shape on missing/out-of-range params.pytest tests/ --ignore=tests/harness).ruff check+ruff format --checkclean on touched files.mypyclean onhal0/errors.py+hal0/api/middleware/error_codes.py.🤖 Generated with Claude Code