@ingeze/api-error
Version:
A TypeScript library for handling HTTP errors in Express, NestJS, and Fastify APIs.
165 lines • 5.12 kB
TypeScript
/**
* Defines custom error classes for handling various "not found" scenarios (HTTP 404).
*
* Each class extends `NotFoundError`, which itself extends `ErrorHandler`.
* These errors represent specific resource-not-found cases and provide
* descriptive messages and error types for easier error handling and debugging.
*
* @module errors/not-found-error
*/
import type { NotFoundErrorType } from '../types/index.js';
import { ErrorHandler } from './error-handler.js';
/**
* Represents a generic "Not Found" error corresponding to HTTP status 404.
*
* This error can be instantiated in two ways:
* 1. By passing an optional `details` object with additional error information.
* In this case, the error message defaults to "Resource not found".
* 2. By passing a custom message string, an optional error type, and optional details.
*
* @extends ErrorHandler
*
* @example
* // Using details object (message defaults to "Resource not found")
* throw new NotFoundError({ reason: 'User does not exist' });
*
* @example
* // Using custom message, type and details
* throw new NotFoundError('User not found', 'NOT_FOUND_USER', { userId: 123 });
*
* @param {Record<string, unknown>} [details] - Optional additional error details.
*
* @param {string} [message] - Optional custom error message.
* @param {NotFoundErrorType} [type='NOT_FOUND'] - Optional specific error type.
* @param {Record<string, unknown>} [details] - Optional additional error details.
*/
export declare class NotFoundError extends ErrorHandler {
constructor(details?: Record<string, unknown>);
constructor(message?: string, type?: NotFoundErrorType, details?: Record<string, unknown>);
}
/**
* Error thrown when a specific user is not found.
*
* Extends the generic NotFoundError with a predefined message and type.
*
* @extends NotFoundError
*
* @example
* // Throw error with optional details
* throw new UserNotFoundError({ userId: '1234' });
*
* @param {Record<string, unknown>} [details] - Optional additional error details.
*/
export declare class UserNotFoundError extends NotFoundError {
/**
* Creates a new UserNotFoundError instance.
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
constructor(details?: Record<string, unknown>);
}
/**
* Error for email not found.
*
* @extends NotFoundError
*
* @example
* throw new EmailNotFoundError({ email: 'user@example.com' });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class EmailNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for product not found.
*
* @extends NotFoundError
*
* @example
* throw new ProductNotFoundError({ productId: 123 });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class ProductNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for post not found.
*
* @extends NotFoundError
*
* @example
* throw new PostNotFoundError({ postId: 456 });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class PostNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for comment not found.
*
* @extends NotFoundError
*
* @example
* throw new CommentNotFoundError({ commentId: 789 });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class CommentNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for category not found.
*
* @extends NotFoundError
*
* @example
* throw new CategoryNotFoundError({ categoryId: 1011 });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class CategoryNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for file not found.
*
* @extends NotFoundError
*
* @example
* throw new FileNotFoundError({ fileName: 'document.pdf' });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class FileNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for image not found.
*
* @extends NotFoundError
*
* @example
* throw new ImageNotFoundError({ imageId: 'img_1234' });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class ImageNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
/**
* Error for address not found.
*
* @extends NotFoundError
*
* @example
* throw new AddressNotFoundError({ addressId: 'addr_5678' });
*
* @param {Record<string, unknown>} [details] Optional additional error details.
*/
export declare class AddressNotFoundError extends NotFoundError {
constructor(details?: Record<string, unknown>);
}
//# sourceMappingURL=not-found-error.d.ts.map