UNPKG

@visulima/error

Version:

Error with more than just a message, stacktrace parsing.

31 lines (29 loc) 1.34 kB
const defaultErrorConstructors = /* @__PURE__ */ new Map([ ["Error", Error], ["EvalError", EvalError], ["RangeError", RangeError], ["ReferenceError", ReferenceError], ["SyntaxError", SyntaxError], ["TypeError", TypeError], ["URIError", URIError] ]); if (typeof AggregateError !== "undefined") { defaultErrorConstructors.set("AggregateError", AggregateError); } const addKnownErrorConstructor = (constructor, name) => { let instance; try { instance = new constructor(); } catch (error) { throw new Error(`The error constructor "${constructor.name}" is not compatible`, { cause: error }); } const resolvedName = name ?? instance.name; if (defaultErrorConstructors.has(resolvedName)) { throw new Error(`The error constructor "${resolvedName}" is already known.`); } defaultErrorConstructors.set(resolvedName, constructor); }; const getKnownErrorConstructors = () => new Map(defaultErrorConstructors); const getErrorConstructor = (name) => defaultErrorConstructors.get(name); const isErrorLike = (value) => value !== null && typeof value === "object" && typeof value.name === "string" && typeof value.message === "string" && (getErrorConstructor(value.name) !== void 0 || value.name === "Error"); export { addKnownErrorConstructor, getErrorConstructor, getKnownErrorConstructors, isErrorLike };