Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.04 KB

File metadata and controls

46 lines (35 loc) · 1.04 KB

Custom Exceptions Guide

Usage Rules

1. Use model-specific exceptions

Good:

raise UserNotFoundError(details="User not found")
raise NotificationNotFoundError(details="Notification not found")

Avoid:

raise DataNotFoundError(details="User not found")  # Too generic

2. Clear and informative messages

Good:

raise UserAuthenticationError(
    details="Invalid credentials. Please check your email/password."
)

3. Migration from Response

Before:

return Response({'detail': 'Error'}, status=404)

After:

raise UserNotFoundError(details="User not found")

Exceptions by Model

  • Users : UserNotFoundError, UserAuthenticationError
  • Products : ProductNotFoundError, ProductOutOfStockError
  • Orders : OrderNotFoundError, OrderValidationError
  • Categories : CategoryNotFoundError
  • Notifications : NotificationNotFoundError, NotificationServiceError
  • Payments : PaymentError, InsufficientFundsError