feat: standardize API error responses on RFC 9457 problem details - #1158
Draft
knowald wants to merge 1 commit into
Draft
feat: standardize API error responses on RFC 9457 problem details#1158knowald wants to merge 1 commit into
knowald wants to merge 1 commit into
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Closes #21. Implements the proposal from my comment there
(#21 (comment)).
Every error the API emits today is one of three incompatible shapes, none carrying a
machine-readable code:
{"detail": "<string>"}from the 105raise HTTPExceptionsites,validation failures converted to a 400 whose string is built from only the first error
(
app/main.py:70+app/utils/exceptions.py:78, so the OpenAPI schema documented a 422array that never hit the wire), and Starlette's plain-text 500 for unhandled exceptions.
Clients have nothing stable to switch on; the dashboard derives error categories from the
HTTP status alone.
All error responses are now RFC 9457 problem details served as
application/problem+json:{ "title": "Not Found", "status": 404, "detail": "User with ID: 123 not found.", "code": "USER_NOT_FOUND" }What changed:
app/utils/problem.py(new): response builder, exception handlers (HTTPException,validation, DatetimeParseError, catch-all 500), and an OpenAPI patch that replaces the
auto-generated
HTTPValidationErrorschema withProblemso spec and wire agree.ApiError(HTTPException)carriescode; all 72 non-webhook raise sites migrated,44 distinct codes. Detail message texts are byte-for-byte unchanged.
"errors": [{field, message, type}]listing everyfailure instead of a 400 with the first one. This is the only deliberate status change.
code: "INTERNAL_ERROR"and a sanitizedmessage. Starlette re-raises after responding, so Sentry and server logs still get the
exception.
detailstring, now withcode: "INVALID_CREDENTIALS"and the sameWWW-Authenticateheader.ApiError.fromResponseparses problem json (backend code onserverCode,422
errorssurfaced in validation messages). Also fixes a latent bug: the clientmatched content type
application/jsonliterally, soapplication/problem+jsonbodieswould have been read as text (
frontend/src/lib/api/client.ts).5 pages updated to values verified against the actual wire output.
Breaking changes for API consumers:
detailfield survives with identical text, soclients reading only
detailkeep working unchanged.Verification: backend suite 1763 passed / 2 skipped; the only pre-existing assertions that
needed changes were the intended 400 -> 422 moves. New
tests/api/v1/test_error_format.pypins the contract: body keys and media type, all-errors 422, login special case with
WWW-Authenticate, status-derived fallback codes for plainHTTPException(404 ->NOT_FOUND), catch-all 500, and/openapi.jsoncontainingProblemwith noHTTPValidationErrorleft. ruff, ty, tsc, and oxlint all clean.Notes:
HTTPException- providers only check status codes. Their responses still render asproblem json via the global handler, with status-derived codes.
moves to 422, and
typeomitted - RFC 9457 defines absence asabout:blank, soresolvable docs URLs can be added later without a breaking change.
timestampfield. A request/trace id extension is the better follow-up if responsecorrelation is ever needed.
str(e)passthroughs) - cleaning those up is a separate concern from the format migration.