Spaces:
Runtime error
Runtime error
File size: 892 Bytes
e4f9cbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
"""Utils for routers."""
import traceback
from typing import Callable
from fastapi import HTTPException, Request, Response
from fastapi.routing import APIRoute
class RouteErrorHandler(APIRoute):
"""Custom APIRoute that handles application errors and exceptions."""
def get_route_handler(self) -> Callable:
"""Get the route handler."""
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
try:
return await original_route_handler(request)
except Exception as ex:
if isinstance(ex, HTTPException):
raise ex
print('Route error:', request.url)
print(ex)
print(traceback.format_exc())
# wrap error into pretty 500 exception
raise HTTPException(status_code=500, detail=traceback.format_exc()) from ex
return custom_route_handler
|