Skip to content

Commit 6afb5c1

Browse files
committed
fix: format aggregate errors
List all errors that make up an aggregate error to make debugging easier
1 parent fb08a2f commit 6afb5c1

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
@@ -77,12 +77,7 @@ debug.formatters.a = (v?: Multiaddr): string => {
7777
return v == null ? 'undefined' : v.toString()
7878
}
7979

80-
// Add a formatter for stringifying Errors
81-
debug.formatters.e = (v?: Error): string => {
82-
if (v == null) {
83-
return 'undefined'
84-
}
85-
80+
function formatError (v: Error): string {
8681
const message = notEmpty(v.message)
8782
const stack = notEmpty(v.stack)
8883

@@ -108,6 +103,35 @@ debug.formatters.e = (v?: Error): string => {
108103
return v.toString()
109104
}
110105

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

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

0 commit comments

Comments
 (0)