Telaio
API Reference

Errors API Reference

Complete reference for Telaio's typed HTTP error classes, ErrorCode enum, and error response format.

Errors API Reference

Import path: telaio/errors

Telaio provides a hierarchy of typed HTTP error classes that map to standard status codes and produce consistent JSON error responses. Throwing any of these from a route handler causes Fastify to return the correct status code automatically.


Error response format

All error classes serialize to this JSON shape:

{
  "status": "error",
  "code": "NOT_FOUND",
  "message": "User not found"
}
FieldTypeValue
statusstringAlways "error"
codestringOne of the ErrorCode enum values
messagestringHuman-readable error description

ErrorCode enum

import { ErrorCode } from 'telaio/errors';

enum ErrorCode {
  ERROR = 'ERROR',
  BAD_REQUEST = 'BAD_REQUEST',
  UNPROCESSABLE_ENTITY = 'UNPROCESSABLE_ENTITY',
  UNAUTHORIZED = 'UNAUTHORIZED',
  FORBIDDEN = 'FORBIDDEN',
  NOT_FOUND = 'NOT_FOUND',
  PAYLOAD_TOO_LARGE = 'PAYLOAD_TOO_LARGE',
}
ValueDescription
ERRORGeneric server error; used by the base RequestError class
BAD_REQUESTThe request body or parameters failed validation
UNPROCESSABLE_ENTITYThe request was well-formed but semantically invalid
UNAUTHORIZEDNo valid authentication credentials were provided
FORBIDDENValid credentials were provided but access is denied
NOT_FOUNDThe requested resource does not exist
PAYLOAD_TOO_LARGEThe request body exceeds the configured size limit

RequestError (base class)

All Telaio error classes extend RequestError. You can catch RequestError to handle any typed HTTP error uniformly.

import { RequestError } from 'telaio/errors';

class RequestError extends Error {
  code: string;
  status: 'error';
  statusCode: number;
  constructor(code?: string, message?: string);
  toJSON(): { status: string; code: string; message: string };
}

Properties

PropertyTypeDefaultDescription
codestring'ERROR'Machine-readable error code
status'error''error'Always the string "error"
statusCodenumber500HTTP status code

Methods

toJSON()

Returns the plain object representation used for JSON serialization:

const err = new RequestError('CUSTOM_CODE', 'Something went wrong');
err.toJSON();
// { status: 'error', code: 'CUSTOM_CODE', message: 'Something went wrong' }

Error subclasses

ClassStatusDefault MessageCode
RequestError500'An error occurred'ERROR
BadRequestError400'Invalid data in request'BAD_REQUEST
UnauthorizedError401'Unauthorized'UNAUTHORIZED
ForbiddenError403'Forbidden'FORBIDDEN
NotFoundError404'Not Found'NOT_FOUND
PayloadTooLargeError413'Payload too large'PAYLOAD_TOO_LARGE

Usage

All subclasses accept an optional custom message:

import {
  BadRequestError,
  NotFoundError,
  UnauthorizedError,
  ForbiddenError,
  PayloadTooLargeError,
} from 'telaio/errors';

// Default message
throw new NotFoundError();
// { status: 'error', code: 'NOT_FOUND', message: 'Not Found' }

// Custom message
throw new NotFoundError('User not found');
// { status: 'error', code: 'NOT_FOUND', message: 'User not found' }

// In a route handler
fastify.get('/users/:id', async (req) => {
  const user = await db.selectFrom('users')
    .where('id', '=', req.params.id)
    .executeTakeFirst();

  if (!user) {
    throw new NotFoundError(`User ${req.params.id} not found`);
  }

  return user;
});

Custom error classes

Extend RequestError to define application-specific error types with custom codes and status codes:

import { RequestError } from 'telaio/errors';

class ConflictError extends RequestError {
  statusCode = 409;

  constructor(message = 'Resource already exists') {
    super('CONFLICT', message);
  }
}

class RateLimitError extends RequestError {
  statusCode = 429;

  constructor(message = 'Too many requests') {
    super('RATE_LIMITED', message);
  }
}

Custom error classes serialize through the same toJSON() mechanism and are handled uniformly by Fastify's error handler. The statusCode property is used to set the HTTP response status.

Custom error with extra fields

If you need to include additional data in the error response, override toJSON():

import { RequestError } from 'telaio/errors';

class ValidationError extends RequestError {
  statusCode = 422;
  readonly fields: Record<string, string>;

  constructor(fields: Record<string, string>) {
    super('VALIDATION_ERROR', 'Validation failed');
    this.fields = fields;
  }

  toJSON() {
    return {
      ...super.toJSON(),
      fields: this.fields,
    };
  }
}

throw new ValidationError({ email: 'must be a valid email address' });
// { status: 'error', code: 'VALIDATION_ERROR', message: 'Validation failed', fields: { email: '...' } }

On this page