UNPKG

api-stds

Version:

Standardized API response and error handling toolkit with async handler, requestId, logging, and configurable formats.

18 lines (17 loc) 832 B
/* eslint-disable @typescript-eslint/no-explicit-any */ import { errorResponse } from "../core/responses.js"; import { ApiError } from "./ApiError.js"; export const asyncHandler = (fn) => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch((error) => { if (error instanceof ApiError) { // If it's a custom ApiError, respect its shape return errorResponse(res, error.statusCode, // string error.message, // message error.errors, // details { requestId: res.locals?.requestId } // meta ); } // Fallback for unexpected errors return errorResponse(res, "INTERNAL_ERROR", error?.message || "Internal Server Error", error?.details || ["An unexpected error occurred"], { requestId: res.locals?.requestId }); }); };