komi-logger
Version:
High-performance, type-safe logging library for Bun with advanced TypeScript body intersection, modular strategy pattern, transform streams, and immutable API design.
90 lines (89 loc) • 2.94 kB
TypeScript
import type { KomiErrorOptions } from './types/komiErrorOptions';
/**
* A custom error class that extends the native {@link Error} class, providing additional properties
* such as a unique identifier, error key, HTTP status code, and cause.
*
* @typeParam T - The type of the cause of the error, which can be any object or error.
*
* @example
* The following example demonstrates how to throw and catch a KomiError.
* ```typescript
* try {
* throw new KomiError({
* message: 'An error occurred',
* key: 'example.error',
* httpStatusCode: 400,
* cause: new Error('Original error')
* });
* } catch (error) {
* if (error instanceof KomiError) {
* console.error(`Error UUID: ${error.uuid}`);
* console.error(`Error Date: ${error.date}`);
* console.error(`Error Key: ${error.key}`);
* console.error(`HTTP Status Code: ${error.httpStatusCode}`);
* console.error(`Cause: ${error.cause}`);
* }
* }
* ```
*
* @example
* The following example demonstrates how to create a KomiError with a custom cause type.
* ```typescript
* const komiError = new KomiError<{ foo: string }>({
* message: 'Custom error with cause',
* key: 'komi-package.error.custom_error',
* httpStatusCode: 500,
* cause: { foo: 'bar' },
* });
* console.log(komiError.cause); // { foo: 'bar' }
* ```
*/
export declare class KomiError<const TCause = unknown> extends Error {
/**
* The cause of the error, typically used to store the original error or additional context.
*/
readonly cause: TCause | undefined;
/**
* The unique identifier of the error, automatically generated using UUID v7.
* This identifier is particularly useful for tracking errors in logs.
*/
private readonly _uuid;
/**
* The date when the error was created, automatically set to the current date and time.
*/
private readonly _date;
/**
* A unique key identifying the type of error, useful for localization or error handling.
*/
private readonly _key;
/**
* The HTTP status code associated with the error, typically used in API responses.
*/
private readonly _httpStatusCode;
/**
* Creates a new instance of the KomiError class.
*
* @param komiErrorOptions - The options for the Komi error. ({@link KomiErrorOptions})
*/
constructor(komiErrorOptions?: Readonly<KomiErrorOptions<TCause>>);
/**
* Gets the unique identifier of the error.
* @returns The UUID of the error.
*/
get uuid(): string;
/**
* Gets the date when the error was created.
* @returns The creation date of the error.
*/
get date(): Date;
/**
* Gets the error key, which identifies the type of error.
* @returns The key associated with the error.
*/
get key(): string;
/**
* Gets the HTTP status code associated with the error.
* @returns The HTTP status code.
*/
get httpStatusCode(): number;
}