Skip to main content
Learn how to consistently handle errors in APIs and provide clear responses.

Error Handling Overview

Consistent Responses

Standardized error format Client-friendly

Status Codes

HTTP status codes RESTful standard

Clear Messages

Developer-friendly User-friendly

Logging

Error tracking Debugging support

SoException

Sonamu provides error classes based on SoException. When you throw a SoException, the framework automatically sends an appropriate HTTP status code and structured error response.

Basic Usage

Available Exception Classes

These are the exception classes provided by Sonamu. All inherit from SoException.

SoException Constructor

All exception classes share the same constructor signature.
  • message: Error message. Supports localized strings (LocalizedString).
  • payload: Additional information. Can pass Zod validation issue arrays, etc.

Usage Examples

Various Error Situations

Passing Detailed Info via payload

isSoException Type Guard

Zod Validation Error Handling

When you pass a Zod issue array to BadRequestException’s payload, Sonamu’s error handler automatically includes validation error details in the response.

Converting Zod Errors

Error Response Format

Sonamu’s built-in error handler responds in the following format.

Basic Error Response

Zod Validation Error Response (when issue array is passed as payload)

Customizing the Error Handler

Sonamu includes a built-in error handler, but you can set a custom error handler via the lifecycle.onError option in startServer if needed.

Status Code Reference

Standard HTTP Status Codes

Sonamu Custom Status Codes

Practical Pattern

Cautions

Cautions for error handling: 1. Never expose sensitive information (stack traces, DB errors, etc.) 2. Use appropriate HTTP status codes 3. Provide clear and consistent error messages 4. Always log errors 5. Limit detailed information in production

Next Steps

Automatic Validation

Zod-based validation

Custom Validation

Custom validation logic

@api Decorator

API basic usage

Context

SonamuContext