Skip to main content
Beyond Sonamu’s built-in exception classes, you can create project-specific exceptions. Inherit from SoException to create exceptions with custom HTTP status codes and messages.

Basic Custom Exceptions

Simple Exception Class

Usage Example:

403 Forbidden Exception

Usage Example:

Business Logic Exceptions

Domain-Specific Exceptions

Usage Example:

State Transition Exceptions

Usage Example:

Limitation Exceptions

Usage Example:

External Service Exceptions

External API Exceptions

Usage Example:

Exception Helper Classes

Exception Factory

Usage Example:

Exception Inheritance Hierarchy

Usage Example:

Exception Grouping

Category-Based Exception Files

Recommendations

HTTP Status Code Selection

Use standard HTTP status codes first, and use custom codes in the 5xx range when needed:
  • 400-499: Client errors (use standard codes)
  • 500-599: Server errors (540-599 for custom use)

Clear Messages

Structured Payload

Exception Documentation