Skip to content

Instantly share code, notes, and snippets.

@aliwo
Created May 4, 2022 01:45
Show Gist options
  • Save aliwo/83230b2be15833042038be6a4128657f to your computer and use it in GitHub Desktop.
Save aliwo/83230b2be15833042038be6a4128657f to your computer and use it in GitHub Desktop.
catching exception
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
class UnknownExceptionMiddleware(BaseHTTPMiddleware): # https://www.starlette.io/middleware/#basehttpmiddleware
def __init__(self, app):
super().__init__(app)
async def dispatch(self, request, call_next):
try:
return await call_next(request)
except Exception:
print("예상치 못한 에러 발생, 로그 찍어야 됩니다.")
return JSONResponse(
message={
"message": "Unknown Server Error",
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment