Skip to content

Commit

Permalink
refactor: add types to error
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasDemea committed Feb 6, 2023
1 parent 3064842 commit 11e20c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
31 changes: 0 additions & 31 deletions lib/error.js

This file was deleted.

45 changes: 45 additions & 0 deletions lib/error.ts
Original file line number Diff line number Diff line change
@@ -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<string, object>[]
) {
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<typeof PlugableError>
) {
return new PlugableError(...args);
});

0 comments on commit 11e20c5

Please sign in to comment.