diff --git a/lib/error.js b/lib/error.js deleted file mode 100644 index 298fc6d..0000000 --- a/lib/error.js +++ /dev/null @@ -1,31 +0,0 @@ -const PlugableError = class PlugableError extends Error { - constructor(code, message, ...contexts) { - if (Array.isArray(message)) { - message = message.filter(function (line) { - return !!line; - }).join(' '); - } - message = `${code}: ${message}`; - super(message); - if (Error.captureStackTrace) { - Error.captureStackTrace(this, PlugableError); - } - this.code = code; - for (const i = 0; i < contexts.length; i++) { - const context = contexts[i]; - for (const key in context) { - if (key === 'code') { - continue; - } - const value = context[key]; - if (value === void 0) { - continue; - } - this[key] = Buffer.isBuffer(value) ? value.toString() : value === null ? value : JSON.parse(JSON.stringify(value)); - } - } - } -}; -export default (function () { - return new PlugableError(...arguments); -}); diff --git a/lib/error.ts b/lib/error.ts new file mode 100644 index 0000000..c091567 --- /dev/null +++ b/lib/error.ts @@ -0,0 +1,45 @@ +const PlugableError = class PlugableError extends Error { + private code: string; + [index: string]: unknown; // required to allow adding class props dynamically + constructor( + code: string, + message: string | (string | undefined)[], + ...contexts: Record[] + ) { + if (Array.isArray(message)) { + message = message + .filter(function (line) { + return !!line; + }) + .join(" "); + } + message = `${code}: ${message}`; + super(message); + if (Error.captureStackTrace) { + Error.captureStackTrace(this, PlugableError); + } + this.code = code; + for (let i = 0; i < contexts.length; i++) { + const context = contexts[i]; + for (const key in context) { + if (key === "code") { + continue; + } + const value = context[key]; + if (value === void 0) { + continue; + } + this[key] = Buffer.isBuffer(value) + ? value.toString() + : value === null + ? value + : JSON.parse(JSON.stringify(value)); + } + } + } +}; +export default (function ( + ...args: ConstructorParameters +) { + return new PlugableError(...args); +});