Handle general exceptions and return standardized error response.
(request: Request, exc: Exception)
| 97 | |
| 98 | |
| 99 | async def handle_general_exception(request: Request, exc: Exception) -> JSONResponse: |
| 100 | """Handle general exceptions and return standardized error response.""" |
| 101 | logger = get_server_logger() |
| 102 | |
| 103 | # Log the error with traceback |
| 104 | logger.log_exception( |
| 105 | exc, |
| 106 | f"General exception occurred: {type(exc).__name__} - {str(exc)}", |
| 107 | correlation_id=getattr(request.state, 'correlation_id', None), |
| 108 | url=str(request.url), |
| 109 | method=request.method |
| 110 | ) |
| 111 | |
| 112 | # For security, don't expose internal error details to the client |
| 113 | error_details = { |
| 114 | "code": "INTERNAL_ERROR", |
| 115 | "message": "An internal server error occurred", |
| 116 | "details": {} # Don't send internal details to client |
| 117 | } |
| 118 | |
| 119 | # In development, we might want to include more details |
| 120 | import os |
| 121 | if os.getenv("ENVIRONMENT") == "development": |
| 122 | error_details["details"]["debug_info"] = { |
| 123 | "exception_type": type(exc).__name__, |
| 124 | "exception_message": str(exc), |
| 125 | "traceback": traceback.format_exc() |
| 126 | } |
| 127 | |
| 128 | return JSONResponse( |
| 129 | status_code=500, |
| 130 | content=jsonable_encoder({"error": error_details}) |
| 131 | ) |
| 132 | |
| 133 | |
| 134 | def add_exception_handlers(app): |
nothing calls this directly
no test coverage detected