File tree Expand file tree Collapse file tree 3 files changed +22
-5
lines changed
packages/logger/src/formatter Expand file tree Collapse file tree 3 files changed +22
-5
lines changed Original file line number Diff line number Diff line change 7676 "engines" : {
7777 "node" : " >=16"
7878 }
79- }
79+ }
Original file line number Diff line number Diff line change @@ -7,6 +7,22 @@ import type {
77import type { UnformattedAttributes } from '../types/Logger.js' ;
88import { LogItem } from './LogItem.js' ;
99
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+
1026/**
1127 * This class defines and implements common methods for the formatting of log attributes.
1228 *
@@ -49,10 +65,11 @@ abstract class LogFormatter implements LogFormatterInterface {
4965 location : this . getCodeLocation ( error . stack ) ,
5066 message : error . message ,
5167 stack : error . stack ,
52- cause :
53- error . cause instanceof Error
68+ cause : isErrorWithCause ( error )
69+ ? error . cause instanceof Error
5470 ? this . formatError ( error . cause )
55- : error . cause ,
71+ : error . cause
72+ : undefined ,
5673 } ;
5774 }
5875
Original file line number Diff line number Diff line change 22 "compilerOptions" : {
33 "incremental" : true ,
44 "composite" : true ,
5- "target" : " ES2022 " , // Node.js 16
5+ "target" : " ES2021 " , // Node.js 16
66 "experimentalDecorators" : true ,
77 "module" : " commonjs" ,
88 "moduleResolution" : " node" ,
You can’t perform that action at this time.
0 commit comments