1- # app/exception_handlers/base.py
21import orjson
32from fastapi import Request
4- from fastapi .responses import JSONResponse
53from rotoger import AppStructLogger
4+ from attrs import define , field
5+
66
77logger = AppStructLogger ().get_logger ()
88
99
10+ @define (slots = True )
11+ class RequestInfo :
12+ """Contains extracted request information."""
13+ path : str = field ()
14+ body : dict = field (default = None )
15+
16+
17+ @define (slots = True )
1018class BaseExceptionHandler :
1119 """Base class for all exception handlers with common functionality."""
1220
1321 @staticmethod
14- async def extract_request_info (request : Request ):
22+ async def extract_request_info (request : Request ) -> RequestInfo :
1523 """Extract common request information."""
1624 request_path = request .url .path
25+ request_body = None
1726 try :
1827 raw_body = await request .body ()
19- request_body = orjson .loads (raw_body ) if raw_body else None
28+ if raw_body :
29+ request_body = orjson .loads (raw_body )
2030 except orjson .JSONDecodeError :
21- request_body = None
31+ pass
2232
23- return request_path , request_body
33+ return RequestInfo ( path = request_path , body = request_body )
2434
2535 @classmethod
26- async def log_error (cls , message , request_info , ** kwargs ):
36+ async def log_error (cls , message : str , request_info : RequestInfo , ** kwargs ):
2737 """Log error with standardized format."""
28- request_path , request_body = request_info
2938 await logger .aerror (
3039 message ,
31- request_url = request_path ,
32- request_body = request_body ,
40+ request_url = request_info . path ,
41+ request_body = request_info . body ,
3342 ** kwargs
3443 )
0 commit comments