Skip to content

Commit dad3cca

Browse files
authored
fix: format aggregate errors (#3337)
List all errors that make up an aggregate error to make debugging easier
1 parent 4bab14e commit dad3cca

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

packages/logger/src/index.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ debug.formatters.a = (v?: Multiaddr): string => {
8080
return v == null ? 'undefined' : v.toString()
8181
}
8282

83-
// Add a formatter for stringifying Errors
84-
debug.formatters.e = (v?: Error): string => {
85-
if (v == null) {
86-
return 'undefined'
87-
}
88-
83+
function formatError (v: Error): string {
8984
const message = notEmpty(v.message)
9085
const stack = notEmpty(v.stack)
9186

@@ -111,6 +106,35 @@ debug.formatters.e = (v?: Error): string => {
111106
return v.toString()
112107
}
113108

109+
function isAggregateError (err?: any): err is AggregateError {
110+
return err?.name === 'AggregateError'
111+
}
112+
113+
// Add a formatter for stringifying Errors
114+
debug.formatters.e = (v?: Error): string => {
115+
if (v == null) {
116+
return 'undefined'
117+
}
118+
119+
if (isAggregateError(v)) {
120+
const indent = ' '
121+
122+
let output = formatError(v)
123+
124+
if (v.errors.length > 0) {
125+
output += `\n${indent}${
126+
v.errors.map(err => ` ${formatError(err).split('\n').join(`\n${indent}`)}`).join(`\n${indent}`)
127+
}`
128+
} else {
129+
output += `\n${indent}[Error list was empty]`
130+
}
131+
132+
return output.trim()
133+
}
134+
135+
return formatError(v)
136+
}
137+
114138
export type { Logger, ComponentLogger }
115139

116140
function createDisabledLogger (namespace: string): debug.Debugger {

0 commit comments

Comments
 (0)