|
2 | 2 | from typing import Dict, Type, cast
|
3 | 3 |
|
4 | 4 | from fastapi import FastAPI
|
| 5 | +from fastapi.exceptions import RequestValidationError |
5 | 6 | from fastapi.middleware.cors import CORSMiddleware
|
6 | 7 | from loguru import logger
|
| 8 | +from starlette.requests import Request |
| 9 | +from starlette.responses import JSONResponse |
7 | 10 | from starlette.staticfiles import StaticFiles
|
| 11 | +from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY |
8 | 12 |
|
9 | 13 | from auth_server.config import AuthServerConfig, ConfigurationError, FlowName, load_config
|
10 | 14 | from auth_server.context import ContextRequestRoute
|
@@ -78,4 +82,16 @@ def init_auth_server_api() -> AuthServer:
|
78 | 82 | app.mount(
|
79 | 83 | "/static", StaticFiles(packages=["auth_server"]), name="static"
|
80 | 84 | ) # defaults to the "statics" directory (the ending s is not a mistake) because starlette says so
|
| 85 | + |
| 86 | + config = load_config() |
| 87 | + if config.debug or config.testing: |
| 88 | + # log more info about 422 errors to ease fault tracing |
| 89 | + @app.exception_handler(RequestValidationError) |
| 90 | + async def validation_exception_handler(request: Request, exc: RequestValidationError): |
| 91 | + |
| 92 | + exc_str = f"{exc}".replace("\n", " ").replace(" ", " ") |
| 93 | + logger.exception(f"{exc}") |
| 94 | + content = {"status_code": 10422, "message": exc_str, "data": None} |
| 95 | + return JSONResponse(content=content, status_code=HTTP_422_UNPROCESSABLE_ENTITY) |
| 96 | + |
81 | 97 | return app
|
0 commit comments