From b218029b2ffe8b71b0d603c8ab575d96f41efa44 Mon Sep 17 00:00:00 2001 From: Nadav Tasher Date: Sun, 31 Mar 2024 21:40:58 +0300 Subject: [PATCH] Exception handlers --- Makefile | 3 +++ image/src/backend/router.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a866b3a..107307e 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,9 @@ clean: test: image $(DOCKER) run --rm -p 80:80 -p 443:443 -e DEBUG=1 $(IMAGE_NAME)/$(IMAGE_TAG) +test-release: image + $(DOCKER) run --rm -p 80:80 -p 443:443 $(IMAGE_NAME)/$(IMAGE_TAG) + test-bash: image $(DOCKER) run --rm -p 80:80 -p 443:443 -e DEBUG=1 -it $(IMAGE_NAME)/$(IMAGE_TAG) bash diff --git a/image/src/backend/router.py b/image/src/backend/router.py index b4ffff2..3c3a31d 100644 --- a/image/src/backend/router.py +++ b/image/src/backend/router.py @@ -1,8 +1,7 @@ import os -import json import inspect import logging -import contextlib +import traceback # Import typing utilities from typing import Union @@ -10,7 +9,7 @@ # Import starlette utilities from starlette.routing import Route, WebSocketRoute from starlette.requests import Request -from starlette.responses import Response, JSONResponse +from starlette.responses import Response, JSONResponse, PlainTextResponse from starlette.websockets import WebSocket from starlette.applications import Starlette @@ -237,5 +236,11 @@ def initialize(): # Set the new logging formatter handler.setFormatter(formatter) + # Create exception handler + exception_handlers = { + # When any exception occurs, return an exception string + Exception: lambda request, exception: PlainTextResponse(str(exception), 500) + } + # Initialize the starlette application - return Starlette(debug=DEBUG, routes=router.routes) + return Starlette(debug=DEBUG, routes=router.routes, exception_handlers=exception_handlers)