Skip to content

Commit 8c02b48

Browse files
committed
add extra logging for 422 errors when testing and debugging
1 parent 813b031 commit 8c02b48

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/auth_server/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
from typing import Dict, Type, cast
33

44
from fastapi import FastAPI
5+
from fastapi.exceptions import RequestValidationError
56
from fastapi.middleware.cors import CORSMiddleware
67
from loguru import logger
8+
from starlette.requests import Request
9+
from starlette.responses import JSONResponse
710
from starlette.staticfiles import StaticFiles
11+
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
812

913
from auth_server.config import AuthServerConfig, ConfigurationError, FlowName, load_config
1014
from auth_server.context import ContextRequestRoute
@@ -78,4 +82,16 @@ def init_auth_server_api() -> AuthServer:
7882
app.mount(
7983
"/static", StaticFiles(packages=["auth_server"]), name="static"
8084
) # 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+
8197
return app

src/auth_server/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class TLSFEDMetadata(BaseModel):
5656
class AuthServerConfig(BaseSettings):
5757
app_name: str = Field(default="auth-server")
5858
environment: Environment = Field(default=Environment.PROD)
59+
debug: bool = False
5960
testing: bool = False
6061
log_level: str = Field(default="INFO")
6162
log_color: bool = True

0 commit comments

Comments
 (0)