UNPKG

@sigiljs/sigil

Version:

TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating

55 lines (54 loc) 1.68 kB
import r from "../../requests/containers/incoming-headers.mjs"; function n(s) { const e = s.replace(/([A-Z])/g, " $1").trim(); return e.charAt(0).toUpperCase() + e.slice(1).toLowerCase(); } class a extends Error { static name = "Exception"; /** * Numeric error code (e.g., HTTP status code between 100 and 599). */ code; headers; /** * Constructs a new Exception. * * @param options configuration for the exception including code, name, and message. */ constructor(e) { super( e.message ? Array.isArray(e.message) ? e.message.join(" ") : e.message : e.name ? n(e.name) : "There was an unknown exception" ), this.headers = new r(e.headers), this.code = e.code >= 100 && e.code <= 599 ? e.code : 500, this.name = e.name ?? "UnknownException"; } /** * Creates an Exception instance from any Error object. * If the error is already an Exception, it is returned directly. * * @param error error to wrap or convert. * @returns An Exception instance with code and message extracted. */ static fromError(e) { return e instanceof a ? e : new a({ name: e.name, code: e.code || 500, message: e.message }); } /** * Describe static exception class. * Used for responses describing in the request meta. * * @param {string} description exception description, will override default description. * @param {Record<string, string>} headers exception headers. * @returns {Exception} exception with internal metadata definition. */ static describe(e, c) { return class extends this { __$description = e; __$headers = c; }; } } export { a as default };