express-error-builder
Version:
Minimal and no setup error builder and producer for express APIs
88 lines (84 loc) • 4.15 kB
text/typescript
import { Response } from 'express';
type LogLevel = 'info' | 'warn' | 'error';
interface ResponseParams {
statusCode: number;
code: string;
message: string;
error: string;
hint?: string;
stack?: string;
type?: string;
instance?: string;
docsLink?: string;
suggestions?: string;
retryable?: boolean;
timestamp?: Date | string;
path?: string;
requestId?: string;
clientMessage?: string;
localizedMessage?: {
[key: string]: string;
};
metaData?: Record<string, string>;
additional?: unknown;
}
interface ResponseParamsWithLog extends ResponseParams {
logMessage?: boolean;
logLevel?: LogLevel;
}
/**
* Builds a standardized error response object for APIs.
*
* @param {Object} - The error response parameters.
* @param {number} statusCode - HTTP status code (e.g., 400, 500).
* @param {string} code - Internal error code identifier.
* @param {string} message - Developer-readable error message.
* @param {string} error - Summary of the error (e.g., "Bad Request").
* @param {string} hint - Optional hint or resolution guide for developers.
* @param {string} stack - Optional stack trace (useful in dev environments).
* @param {string} type - Optional URI reference for error type (RFC 7807 style).
* @param {string} instance - Optional unique error instance identifier.
* @param {string} docsLink - Optional link to API or error documentation.
* @param {string} suggestions - Optional suggestions to fix the error.
* @param {boolean} retryable - Whether the operation can be retried.
* @param {Date|string} timestamp - Timestamp of the error (default: now).
* @param {string} path - Request path where the error occurred.
* @param {string} requestId - Request identifier for tracing.
* @param {string} clientMessage - User-friendly message for clients.
* @param {Object.<string, string>} localizedMessage - Translated messages keyed by locale (e.g., { en, fr }).
* @param {Object.<string, string>} metaData - Optional metadata to provide additional context.
* @param {*} additional - Any additional custom data.
*
* @returns {Object} Formatted error response object.
*/
declare const errorJsonBuilder: (options: ResponseParams) => ResponseParams;
/**
* Produces a standardized error response for Express APIs and logs the error conditionally.
*
* @param {Response} response - Express response object used to send the error response.
* @param {Object}- Configuration object for the error.
* @param {number}statusCode - HTTP status code (e.g., 400, 500).
* @param {string}code - Internal error code.
* @param {string}message - Developer-readable error message.
* @param {string}error - Short error title (e.g., "Bad Request").
* @param {string} hint - Developer hint for fixing the error.
* @param {string} stack - Stack trace (useful for debugging).
* @param {string} type - URI reference that identifies the error type.
* @param {string} instance - Unique error instance identifier.
* @param {string} docsLink - Link to documentation related to the error.
* @param {string} suggestions - Suggestions for resolving the issue.
* @param {boolean} retryable - Indicates whether the request can be retried.
* @param {Date|string} timestamp - When the error occurred. Defaults to now.
* @param {string} path - The request path that triggered the error.
* @param {string} requestId - Unique identifier for the request.
* @param {string} clientMessage - User-friendly message intended for the client.
* @param {Object.<string, string>} localizedMessage - Translations for different locales.
* @param {Object.<string, string>} metaData - Any metadata to include in the response.
* @param {*} additional - Additional custom data.
* @param {boolean} logMessage - Whether to log the error to the console.
* @param {"info"|"warn"|"error"} logLevel - Level of the log (controls color).
*
* @returns {void}
*/
declare const produceErrorResponse: (response: Response, options: ResponseParamsWithLog) => void;
export { type LogLevel, type ResponseParams, type ResponseParamsWithLog, errorJsonBuilder, produceErrorResponse };