Skip to content

fix: return all errors as JSON instead of HTML pages - #79

Merged
firefly-cpp merged 1 commit into
mainfrom
fix/json-errors
Nov 8, 2026
Merged

firefly-cpp merged 1 commit into
mainfrom
fix/json-errors

Conversation

@firefly-cpp

Copy link
Copy Markdown
Owner

Problem

The API's own errors are JSON ({"message": ...}), but errors raised by Flask itself were returned as HTML error pages:

Request Before After
Invalid or empty JSON body 400, HTML 400 {"message": "Invalid JSON: the request body could not be parsed."}
Unknown path 404, HTML 404 {"message": "Not Found."}
Wrong method (e.g. DELETE /measure) 405, HTML 405 {"message": "Method Not Allowed."}, Allow header kept
Unexpected error in the server 500, HTML 500 {"message": "Internal server error."}, logged with traceback
Payload too large 413, JSON unchanged
GET / (index page) 200, HTML unchanged

Changes

  • SucculentAPI.http_error() handles every HTTPException and 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 a BadRequest with 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.
  • README and getting started mention that all errors are JSON with a message field.

Tests

  • New TestJsonErrors (invalid/empty JSON, 404, 405 with Allow, unexpected error logged and hidden, existing 413 unchanged, index still HTML). Five of them fail on the previous code.
  • Full suite: 51 passed.

Compatibility

Status codes are unchanged; only the body and content type of these error responses change from HTML to JSON.


Generated by Claude Code

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
@firefly-cpp
firefly-cpp merged commit 47ce26d into main Nov 8, 2026
12 checks passed
@firefly-cpp
firefly-cpp deleted the fix/json-errors branch November 8, 2026 19:23
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