From d09ec0da7458e20dfa445a97a33ae6f4dc172d83 Mon Sep 17 00:00:00 2001 From: Username Date: Thu, 25 Dec 2025 19:20:08 +0100 Subject: [PATCH] fix: handle 405 Method Not Allowed properly Exception handler was catching MethodNotAllowed and returning 500. Added dedicated 405 handler and passthrough for HTTPException. --- app/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index c9e346d..384fb20 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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", "-") )