Skip to main content

@adrper79-dot/errors

Typed error classes and utilities used across all Factory packages. No dependencies.

Installation

npm install @adrper79-dot/errors

API

FactoryError

Base class for all Factory errors. Extends Error with a machine-readable code and optional context bag.
class FactoryError extends Error {
  readonly code: string;
  readonly context: Record<string, unknown>;
  readonly statusCode: number;
}

Error subclasses

ClassstatusCodeDefault code
ValidationError400VALIDATION_ERROR
AuthError401AUTH_ERROR
ForbiddenError403FORBIDDEN
NotFoundError404NOT_FOUND
ConflictError409CONFLICT
RateLimitError429RATE_LIMIT
InternalError500INTERNAL_ERROR

toErrorResponse(error)

Serializes any FactoryError into a JSON-safe response envelope:
function toErrorResponse(error: FactoryError): ErrorResponse;

type ErrorResponse = {
  error: {
    code: string;
    message: string;
    context?: Record<string, unknown>;
  };
};

ErrorCodes

Constant map of all well-known error code strings:
import { ErrorCodes } from '@adrper79-dot/errors';
// ErrorCodes.AUTH_TOKEN_INVALID, ErrorCodes.AUTH_TOKEN_EXPIRED, etc.

Example

import { ValidationError, toErrorResponse } from '@adrper79-dot/errors';

throw new ValidationError('Email is required', {
  code: 'VALIDATION_EMAIL_REQUIRED',
  context: { field: 'email' },
});