Skip to content

Commit

Permalink
add extra logging for 422 errors when testing and debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
johanlundberg committed May 3, 2024
1 parent 813b031 commit 8c02b48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/auth_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
from typing import Dict, Type, cast

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.staticfiles import StaticFiles
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY

from auth_server.config import AuthServerConfig, ConfigurationError, FlowName, load_config
from auth_server.context import ContextRequestRoute
Expand Down Expand Up @@ -78,4 +82,16 @@ def init_auth_server_api() -> AuthServer:
app.mount(
"/static", StaticFiles(packages=["auth_server"]), name="static"
) # defaults to the "statics" directory (the ending s is not a mistake) because starlette says so

config = load_config()
if config.debug or config.testing:
# log more info about 422 errors to ease fault tracing
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):

exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logger.exception(f"{exc}")
content = {"status_code": 10422, "message": exc_str, "data": None}
return JSONResponse(content=content, status_code=HTTP_422_UNPROCESSABLE_ENTITY)

return app
1 change: 1 addition & 0 deletions src/auth_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class TLSFEDMetadata(BaseModel):
class AuthServerConfig(BaseSettings):
app_name: str = Field(default="auth-server")
environment: Environment = Field(default=Environment.PROD)
debug: bool = False
testing: bool = False
log_level: str = Field(default="INFO")
log_color: bool = True
Expand Down

0 comments on commit 8c02b48

Please sign in to comment.