Skip to content

Commit

Permalink
Exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
NadavTasher committed Mar 31, 2024
1 parent f60ee88 commit b218029
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions image/src/backend/router.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import json
import inspect
import logging
import contextlib
import traceback

# Import typing utilities
from typing import Union

# 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

Expand Down Expand Up @@ -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)

0 comments on commit b218029

Please sign in to comment.