-
Notifications
You must be signed in to change notification settings - Fork 55
/
error-with-cause.ts
41 lines (37 loc) · 1.54 KB
/
error-with-cause.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { unixEOL } from './string-formatter';
/**
* Represents an error that was caused by another error.
*/
export class ErrorWithCause extends Error {
/**
* Create an instance of ErrorWithCause.
* @param message Error message.
* @param cause Original error, causing this error.
*/
constructor(message: string, public readonly cause: Error) {
// There is an issue with the prototype chain when extending from Error: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
super(message); // 'Error' breaks prototype chain here
Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
this.name = 'ErrorWithCause';
// Stack is a non-standard property according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types
if (this.stack && cause?.stack) {
this.stack = `${this.stack}${unixEOL}Caused by:${unixEOL}${cause.stack}`;
}
}
/**
* Root cause of the error.
* If there are multiple errors caused one by another, the root cause is the first error that occurred.
* In case there is no root cause.
*/
get rootCause(): Error {
return isErrorWithCause(this.cause) ? this.cause.rootCause : this.cause;
}
}
/**
* Type guard to check whether an error is of type ErrorWithCause.
* @param err An error.
* @returns Whether the given error is of type ErrorWithCause.
*/
export function isErrorWithCause(err: Error): err is ErrorWithCause {
return err?.name === 'ErrorWithCause';
}