@@ -430,7 +430,6 @@ First, you may want to take a look at the project structure and understand what
430430 │ ├── api # Folder containing API-related logic.
431431 │ │ ├── __init__.py
432432 │ │ ├── dependencies.py # Defines dependencies for use across API endpoints.
433- │ │ ├── exceptions.py # Custom exceptions for the API.
434433 │ │ ├── paginated.py # Utilities for API response pagination.
435434 │ │ │
436435 │ │ └── v1 # Version 1 of the API.
@@ -460,7 +459,8 @@ First, you may want to take a look at the project structure and understand what
460459 │ │ │
461460 │ │ ├── exceptions # Custom exception classes.
462461 │ │ │ ├── __init__.py
463- │ │ │ └── exceptions.py # Definitions of custom exceptions.
462+ │ │ │ ├── cache_exceptions.py # Exceptions related to cache operations.
463+ │ │ │ └── http_exceptions.py # HTTP-related exceptions.
464464 │ │ │
465465 │ │ └── utils # Utility functions and helpers.
466466 │ │ ├── __init__.py
@@ -890,6 +890,33 @@ async def read_entities(
890890 )
891891```
892892
893+ #### 5.7.2 HTTP Exceptions
894+
895+ To add exceptions you may just import from ` app/core/exceptions/http_exceptions ` and optionally add a detail:
896+
897+ ``` python
898+ from app.core.exceptions.http_exceptions import NotFoundException
899+
900+ # If you want to specify the detail, just add the message
901+ if not user:
902+ raise NotFoundException(" User not found" )
903+
904+ # Or you may just use the default message
905+ if not post:
906+ raise NotFoundException()
907+ ```
908+
909+ ** The predefined possibilities in http_exceptions are the following:**
910+ - ` CustomException ` : 500 internal error
911+ - ` BadRequestException ` : 400 bad request
912+ - ` NotFoundException ` : 404 not found
913+ - ` ForbiddenException ` : 403 forbidden
914+ - ` UnauthorizedException ` : 401 unauthorized
915+ - ` UnprocessableEntityException ` : 422 unprocessable entity
916+ - ` DuplicateValueException ` : 422 unprocessable entity
917+ - ` RateLimitException ` : 429 too many requests
918+
919+
893920### 5.8 Caching
894921The ` cache ` decorator allows you to cache the results of FastAPI endpoint functions, enhancing response times and reducing the load on your application by storing and retrieving data in a cache.
895922
0 commit comments