fix: handle 405 Method Not Allowed properly

Exception handler was catching MethodNotAllowed and returning 500.
Added dedicated 405 handler and passthrough for HTTPException.
This commit is contained in:
Username
2025-12-25 19:20:08 +01:00
parent 0a7627fbe5
commit d09ec0da74

View File

@@ -181,6 +181,15 @@ def setup_error_handlers(app: Flask) -> None:
mimetype="application/json",
)
@app.errorhandler(405)
def method_not_allowed(error: HTTPException) -> Response:
"""Handle 405 Method Not Allowed errors."""
return Response(
json.dumps({"error": "Method not allowed"}),
status=405,
mimetype="application/json",
)
@app.errorhandler(500)
def internal_error(error: HTTPException) -> Response:
"""Handle 500 Internal Server errors."""
@@ -199,6 +208,9 @@ def setup_error_handlers(app: Flask) -> None:
@app.errorhandler(Exception)
def handle_exception(error: Exception) -> Response:
"""Handle unhandled exceptions with generic 500 response."""
# Re-raise HTTP exceptions to their proper handlers
if isinstance(error, HTTPException):
return error # type: ignore[return-value]
app.logger.exception(
"Unhandled exception: %s [rid=%s]", str(error), getattr(g, "request_id", "-")
)