presentation.routers.api.v1.errors.exception_handlers¶
src.presentation.routers.api.v1.errors.exception_handlers
¶
Global exception handlers for FastAPI application.
This module provides exception handlers that catch unhandled exceptions and convert them to RFC 9457 Problem Details responses.
Handlers
http_exception_handler: Converts HTTPException to RFC 9457 format validation_exception_handler: Converts RequestValidationError to RFC 9457 format generic_exception_handler: Catches all unhandled exceptions
Exports
register_exception_handlers: Register all exception handlers with FastAPI app
Classes¶
Functions¶
http_exception_handler
async
¶
Convert HTTPException to RFC 9457 Problem Details response.
This handler ensures all HTTPException responses (e.g., from auth dependencies) are returned in RFC 9457 format for consistency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI Request object. |
required |
exc
|
Exception
|
HTTPException raised by handler or dependency. |
required |
Returns:
| Type | Description |
|---|---|
JSONResponse
|
JSONResponse with RFC 9457 ProblemDetails. |
Example
When auth dependency raises HTTPException:¶
raise HTTPException(status_code=401, detail="Invalid token")
Returns RFC 9457 response:¶
{¶
"type": "https://api.dashtam.com/errors/unauthorized",¶
"title": "Authentication Required",¶
"status": 401,¶
"detail": "Invalid token",¶
"instance": "/api/v1/accounts",¶
"trace_id": "..."¶
}¶
Source code in src/presentation/routers/api/v1/errors/exception_handlers.py
validation_exception_handler
async
¶
Convert RequestValidationError to RFC 9457 Problem Details response.
This handler ensures Pydantic validation errors are returned in RFC 9457 format with structured field-level errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI Request object. |
required |
exc
|
Exception
|
RequestValidationError from Pydantic validation. |
required |
Returns:
| Type | Description |
|---|---|
JSONResponse
|
JSONResponse with RFC 9457 ProblemDetails including field errors. |
Example
When Pydantic validation fails:¶
POST /api/v1/users with invalid email¶
Returns RFC 9457 response:¶
{¶
"type": "https://api.dashtam.com/errors/validation-failed",¶
"title": "Validation Failed",¶
"status": 422,¶
"detail": "Request validation failed",¶
"instance": "/api/v1/users",¶
"errors": [¶
{"field": "email", "code": "value_error", "message": "Invalid email"}¶
],¶
"trace_id": "..."¶
}¶
Source code in src/presentation/routers/api/v1/errors/exception_handlers.py
generic_exception_handler
async
¶
Handle unexpected Python exceptions.
Converts any unhandled exception into RFC 9457 Problem Details response. Prevents leaking stack traces or internal details to API consumers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI Request object |
required |
exc
|
Exception
|
Unhandled exception |
required |
Returns:
| Type | Description |
|---|---|
JSONResponse
|
JSONResponse with RFC 9457 ProblemDetails (500 Internal Server Error) |
Example
When any unhandled exception occurs:¶
raise ValueError("Something went wrong")
Returns RFC 9457 response with trace_id for debugging¶
Source code in src/presentation/routers/api/v1/errors/exception_handlers.py
register_exception_handlers
¶
Register all exception handlers with FastAPI application.
This function should be called during application initialization to register global exception handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application instance |
required |
Example
from fastapi import FastAPI app = FastAPI() register_exception_handlers(app)