Overview
@contract-kit/errors provides a type-safe error catalog system and error handling utilities for building robust APIs. It includes an AppError class for domain/application errors and automatic integration with @contract-kit/next.
Prefer the
contract-kit meta package for new projects. It re-exports
@contract-kit/errors along with core, client, application, and more.Installation
Features
- Error Catalog - Define application errors in a central location with HTTP status codes
- Type-Safe Errors - Full TypeScript support for error definitions
- AppError Class - Throwable error class for domain and application code
- Error Factory - Type-safe factory for creating errors from your catalog
- Next.js Integration - Automatic error handling in
@contract-kit/next - HTTP Error Helpers - Pre-built catalog of common HTTP errors
Usage
Defining an Error Catalog
Create a central error catalog for your application:Throwing Errors in Use Cases
Use the error factory to throw type-safe errors:Adding Error Details
You can include additional context in error details:Error Response Format
When anAppError is thrown, it’s automatically converted to a JSON response:
404 for TodoNotFound).
HTTP Error Helpers
Start from a base catalog of common HTTP errors:Available HTTP Errors
ThehttpErrors catalog includes:
| Error | Status | Code |
|---|---|---|
BadRequest | 400 | BAD_REQUEST |
Unauthorized | 401 | UNAUTHORIZED |
Forbidden | 403 | FORBIDDEN |
NotFound | 404 | NOT_FOUND |
Conflict | 409 | CONFLICT |
UnprocessableEntity | 422 | UNPROCESSABLE_ENTITY |
InternalServerError | 500 | INTERNAL_SERVER_ERROR |
Next.js Integration
AppError instances are automatically handled by @contract-kit/next:
AppError is thrown from a handler:
- It’s automatically caught by the Next.js adapter
- Converted to a JSON response with the error’s status code
- Returned to the client with the standard format
API Reference
defineErrors<T>(defs: T): T
Type-preserving helper to define an error catalog. Returns the same object with full type information.
createErrorFactory<T>(catalog: T): ErrorFactory<T>
Creates an error factory bound to a specific catalog.
AppError
Error class that can be thrown from domain/application code.
isAppError(err: unknown): err is AppError
Type guard to check if an error is an AppError.
toErrorResponseBody(err: AppError): ErrorResponseBody
Converts an AppError to a standard error response body.
createErrorResponseSchema(schemaBuilder: () => TSchema): TSchema
Helper for creating error response schemas using your preferred schema library.
httpErrors
A base catalog of common HTTP-related errors.
HttpErrorCatalog
TypeScript type of the default HTTP error catalog.
Best Practices
Use descriptive error codes
Use descriptive error codes
Error codes should be SCREAMING_SNAKE_CASE and clearly describe the problem:
TODO_NOT_FOUND, INVALID_EMAIL_FORMAT, INSUFFICIENT_PERMISSIONS.Include helpful details
Include helpful details
Add context in the
details field to help with debugging: resource IDs, validation errors, or any relevant data.Map HTTP status codes correctly
Map HTTP status codes correctly
Use appropriate HTTP status codes: 400 for client errors, 401 for authentication, 403 for authorization, 404 for not found, 409 for conflicts, 422 for validation errors, 500 for server errors.
Centralize error definitions
Centralize error definitions
Keep all error definitions in one place (e.g.,
app/errors.ts) for consistency and easy maintenance.Extend httpErrors for common cases
Extend httpErrors for common cases
Start with the base
httpErrors catalog and add your domain-specific errors to avoid duplicating common HTTP errors.