File tree 3 files changed +22
-5
lines changed
packages/logger/src/formatter
3 files changed +22
-5
lines changed Original file line number Diff line number Diff line change 76
76
"engines" : {
77
77
"node" : " >=16"
78
78
}
79
- }
79
+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,22 @@ import type {
7
7
import type { UnformattedAttributes } from '../types/Logger.js' ;
8
8
import { LogItem } from './LogItem.js' ;
9
9
10
+ /**
11
+ * Typeguard to monkey patch Error to add a cause property.
12
+ *
13
+ * This is needed because the `cause` property is present in ES2022 or newer.
14
+ * Since we want to be able to format errors in Node 16.x, we need to
15
+ * add this property ourselves. We can remove this once we drop support
16
+ * for Node 16.x.
17
+ *
18
+ * @see https://nodejs.org/api/errors.html#errors_error_cause
19
+ */
20
+ const isErrorWithCause = (
21
+ error : Error
22
+ ) : error is Error & { cause : unknown } => {
23
+ return 'cause' in error ;
24
+ } ;
25
+
10
26
/**
11
27
* This class defines and implements common methods for the formatting of log attributes.
12
28
*
@@ -49,10 +65,11 @@ abstract class LogFormatter implements LogFormatterInterface {
49
65
location : this . getCodeLocation ( error . stack ) ,
50
66
message : error . message ,
51
67
stack : error . stack ,
52
- cause :
53
- error . cause instanceof Error
68
+ cause : isErrorWithCause ( error )
69
+ ? error . cause instanceof Error
54
70
? this . formatError ( error . cause )
55
- : error . cause ,
71
+ : error . cause
72
+ : undefined ,
56
73
} ;
57
74
}
58
75
Original file line number Diff line number Diff line change 2
2
"compilerOptions" : {
3
3
"incremental" : true ,
4
4
"composite" : true ,
5
- "target" : " ES2022 " , // Node.js 16
5
+ "target" : " ES2021 " , // Node.js 16
6
6
"experimentalDecorators" : true ,
7
7
"module" : " commonjs" ,
8
8
"moduleResolution" : " node" ,
You can’t perform that action at this time.
0 commit comments