Shared application error types for TypeScript services. The package keeps
transport details out of the core model and focuses on a portable payload:
code, message, details, and localizedMessage.
import { BadRequest } from 'unify-errors';
function errorExample() {
throw new BadRequest('INVALID_AMOUNT', {
message: 'Amount must be positive',
details: ['Got: -5'],
localizedMessage: 'Amount must be greater than zero',
});
}All built-in error classes extend CustomError.
new BadRequest(code, {
message,
details,
localizedMessage,
});| Field | Type | Description |
|---|---|---|
message |
string |
Technical message intended for logs or debugging |
details |
string[] |
Optional detail lines that adapters can expose or strip |
localizedMessage |
string |
Optional user-facing message |
CustomError#toResponse(includeDetails?) serializes the shared payload:
{
code?: string;
message?: string;
details: string[];
localizedMessage?: string;
}BadRequestUnauthorizedForbiddenNotFoundConflictTimeOutTooManyRequestsInternalServerErrorNotImplementedServiceUnavailable
Create a new class by extending CustomError and keeping transport status
mapping in your adapter layer.
import { CustomError, type CustomErrorOptions } from 'unify-errors';
export class PaymentDeclined extends CustomError {
constructor(code: string, options: CustomErrorOptions = {}) {
super(code, options);
Object.setPrototypeOf(this, PaymentDeclined.prototype);
this.name = 'PaymentDeclined';
}
}bun test