Repository navigation
fix: return all errors as JSON instead of HTML pages - #79
Merged
Merged
Conversation
Errors raised by Flask itself were returned as HTML error pages, while
the API's own errors are JSON ({"message": ...}).
- Return HTTP errors (e.g. invalid JSON body, 404, 405) as JSON with a
short message; headers such as Allow (405) are kept
- Give invalid or empty JSON bodies a clear message:
"Invalid JSON: the request body could not be parsed."
- Return unexpected errors as a JSON 500 "Internal server error." and
log them with the traceback, without exposing details to the client
- Keep the 413 "Payload too large." response and the HTML index page
- Document the error format and add tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
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.
Problem
The API's own errors are JSON (
{"message": ...}), but errors raised by Flask itself were returned as HTML error pages:{"message": "Invalid JSON: the request body could not be parsed."}{"message": "Not Found."}DELETE /measure){"message": "Method Not Allowed."},Allowheader kept{"message": "Internal server error."}, logged with tracebackGET /(index page)Changes
SucculentAPI.http_error()handles everyHTTPExceptionand returns it as JSON. Werkzeug's generic descriptions are replaced with the short error name (e.g.Not Found.); specific descriptions are kept. Response headers are kept.SucculentRequest(set as the app's request class) raises aBadRequestwith a clear message when a JSON body cannot be parsed.SucculentAPI.internal_error()handles unexpected exceptions: it logs the traceback and returns a generic JSON 500 without exposing details to the client.messagefield.Tests
TestJsonErrors(invalid/empty JSON, 404, 405 withAllow, unexpected error logged and hidden, existing 413 unchanged, index still HTML). Five of them fail on the previous code.Compatibility
Status codes are unchanged; only the body and content type of these error responses change from HTML to JSON.
Generated by Claude Code