Skip to content

Commit f89b71a

Browse files
committed
Init err.cause only if it exists
1 parent 4c2fd8b commit f89b71a

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/chain-error.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
99
* Indicates that the new error was caused by `cause`. See `getCause()` below.
1010
* If unspecified, the cause will be `null`.
1111
*/
12-
override readonly cause?: Error;
12+
declare cause?: Error;
1313
/**
1414
* Specifies arbitrary informational properties that
1515
* are available through the `ChainError.getInfo(err)` static class method.
@@ -56,7 +56,9 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
5656
* If we've been given a cause, record a reference to it and update our
5757
* message appropriately.
5858
*/
59-
this.cause = options.cause;
59+
if (options.cause) {
60+
this.cause = options.cause;
61+
}
6062
if (this.cause && !skipCauseMessage) {
6163
this.message += ': ' + this.cause.message;
6264
}
@@ -154,7 +156,7 @@ export class ChainError<T extends ObjectAny = ObjectAny> extends Error {
154156
* Returns a string containing the full stack trace, with all nested errors recursively
155157
* reported as `'caused by:' + err.stack`.
156158
*/
157-
static getFullStack(err: { cause?: unknown, stack?: string }): string | undefined {
159+
static getFullStack(err: { cause?: unknown; stack?: string }): string | undefined {
158160
const cause = err.cause;
159161
if (cause) {
160162
return err.stack + '\ncaused by: ' + this.getFullStack(cause);

0 commit comments

Comments
 (0)