Skip to content

Conversation

igorbenav
Copy link
Collaborator

Exception handling was restructured.

Old exceptions structure:

├── app                          # Main application directory.
   ├── ...
   │
   ├── api                       # Folder containing API-related logic.
   │   ├── ...
   │   ├── exceptions.py         # Custom exceptions for the API.
   │
   ├── core                      # Core utilities and configurations for the application.
   │   ├── ...
   │   │
   │   ├── exceptions            # Custom exception classes.
   │   │   ├── __init__.py
   │   │   └── exceptions.py     # Definitions of custom exceptions.

New structure:

├── app                              # Main application directory.
   ├── ...
   │
   ├── core                          # Core utilities and configurations for the application.
   │   ├── ...
   │   │
   │   ├── exceptions                # Custom exception classes.
   │   │   ├── __init__.py
   │   │   ├── cache_exceptions.py   # Exceptions related to cache operations.
   │   │   └── http_exceptions.py    # HTTP-related exceptions.

Now to use http exceptions you may just import from app/core/exceptions/http_exceptions and optionally add a detail:

from app.core.exceptions.http_exceptions import NotFoundException

# If you want to specify the detail, just add the message
if not user:
  raise NotFoundException("User not found")

# Or you may just use the default message
if not post:
  raise NotFoundException()

The predefined possibilities in http_exceptions are the following:

  • CustomException: 500 internal error
  • BadRequestException: 400 bad request
  • NotFoundException: 404 not found
  • ForbiddenException: 403 forbidden
  • UnauthorizedException: 401 unauthorized
  • UnprocessableEntityException: 422 unprocessable entity
  • DuplicateValueException: 422 unprocessable entity
  • RateLimitException: 429 too many requests

@igorbenav igorbenav added the enhancement New feature or request label Nov 24, 2023
@igorbenav igorbenav self-assigned this Nov 24, 2023
@igorbenav igorbenav merged commit b31aa9e into main Nov 24, 2023
@igorbenav igorbenav deleted the better-exceptions branch November 24, 2023 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant