Skip to content

BUG: Add global error handlers for 404, 405, and unhandled 500 to return consistent JSON #41

Description

@DewaldOosthuizen

Summary

Flask returns HTML error pages by default for 404 Not Found, 405 Method Not Allowed, and unhandled 500 Internal Server Error. All other endpoints in this API return JSON. Mixing HTML error responses with JSON success responses forces clients to inspect Content-Type before parsing, and is inconsistent with the API contract.

Background

A uniform error response format is a REST best practice. Clients (including the tests and Postman users) should always receive JSON with a status and msg field regardless of the error type. This is especially relevant since the tutorial targets developers learning API design.

Affected Areas

  • web/app.py — no @app.errorhandler registrations present
  • web/tests/test_app.py — no tests for 404/405 responses

Recommended Fix

@app.errorhandler(404)
def not_found(e):
    return jsonify({"status": 404, "msg": "Not found"}), 404

@app.errorhandler(405)
def method_not_allowed(e):
    return jsonify({"status": 405, "msg": "Method not allowed"}), 405

@app.errorhandler(500)
def internal_error(e):
    return jsonify({"status": 500, "msg": "Internal server error"}), 500

Acceptance Criteria

  • GET /register returns 405 JSON (not HTML)
  • GET /nonexistent returns 404 JSON (not HTML)
  • All error responses share the {"status": N, "msg": "..."} shape
  • Tests added for 404 and 405 cases

Complexity Estimate

S — three handler functions plus test cases.

Priority

Medium — improves API consistency and client developer experience.


Auto-identified by workspace issue-logger
Category: error handling / resilience
Complexity: S
Repository: DewaldOosthuizen/python_rest_tutorial

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibugSomething isn't workingenhancementNew feature or requestpythonPull requests that update python code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions