-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3064842
commit 11e20c5
Showing
2 changed files
with
45 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |