Skip to main content
SoException is the abstract base class that all Sonamu exception classes inherit from. It can include HTTP status codes, messages, and additional payloads.

Type Definition

Properties

statusCode

HTTP response status code. When an exception is thrown, Fastify responds with this code.

message

Exception message. Uses the LocalizedString type, delivering localized strings through Sonamu’s i18n system.

payload

Optional additional data. Used to convey detailed information related to the exception. Usage Example:

Utility Functions

isSoException()

Checks whether a given value is an instance of SoException.
Usage Example:

Built-in Exception Classes

Sonamu provides several built-in exception classes for common HTTP error scenarios.

BadRequestException (400)

Used when there are issues with request parameters or other request details.
Usage Example:

UnauthorizedException (401)

Used when login is required but the user is logged out, or when access permission is lacking.
Usage Example:

NotFoundException (404)

Used when accessing a non-existent record.
Usage Example:

InternalServerErrorException (500)

Used when internal processing logic errors occur (including external API calls).
Usage Example:

ServiceUnavailableException (503)

Used when processing is impossible in the current state.
Usage Example:

TargetNotFoundException (520)

Used when there is no target for the operation.
Usage Example:

AlreadyProcessedException (541)

Used when attempting duplicate processing of an already processed request.
Usage Example:

DuplicateRowException (542)

Used when duplicate requests are made where duplicates are not allowed.
Usage Example:

Exception Handling Flow

Sonamu automatically processes thrown exceptions and converts them into appropriate HTTP responses:
When payload is present:

Exceptions vs Guards

For simple authentication checks, it’s better to use Guards: