Skip to content

Commit 8cebf63

Browse files
committed
fix: log aggregate errors recursively
1 parent 0bbf502 commit 8cebf63

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

packages/logger/src/index.ts

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

83-
function formatError (v: Error): string {
83+
function formatError (v: Error, indent = ''): string {
8484
const message = notEmpty(v.message)
8585
const stack = notEmpty(v.stack)
8686

@@ -89,41 +89,38 @@ function formatError (v: Error): string {
8989
// sometimes is isn't so try to do *something* useful
9090
if (message != null && stack != null) {
9191
if (stack.includes(message)) {
92-
return stack
92+
return `${stack.split('\n').join(`\n${indent}`)}`
9393
}
9494

95-
return `${message}\n${stack}`
95+
return `${message}\n${indent}${stack.split('\n').join(`\n${indent}`)}`
9696
}
9797

9898
if (stack != null) {
99-
return stack
99+
return `${stack.split('\n').join(`\n${indent}`)}`
100100
}
101101

102102
if (message != null) {
103-
return message
103+
return `${message}`
104104
}
105105

106-
return v.toString()
106+
return `${v.toString()}`
107107
}
108108

109109
function isAggregateError (err?: any): err is AggregateError {
110110
return err instanceof AggregateError || (err?.name === 'AggregateError' && Array.isArray(err.errors))
111111
}
112112

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 = ' '
113+
function printError (err: Error, indent = ''): string {
114+
if (isAggregateError(err)) {
115+
let output = formatError(err, indent)
121116

122-
let output = formatError(v)
117+
if (err.errors.length > 0) {
118+
indent = `${indent} `
123119

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}`)
120+
output += `${
121+
err.errors
122+
.map(err => `\n${indent}${printError(err, `${indent}`)}`)
123+
.join(`\n${indent}`)
127124
}`
128125
} else {
129126
output += `\n${indent}[Error list was empty]`
@@ -132,7 +129,16 @@ debug.formatters.e = (v?: Error): string => {
132129
return output.trim()
133130
}
134131

135-
return formatError(v)
132+
return formatError(err, indent)
133+
}
134+
135+
// Add a formatter for stringifying Errors
136+
debug.formatters.e = (v?: Error): string => {
137+
if (v == null) {
138+
return 'undefined'
139+
}
140+
141+
return printError(v)
136142
}
137143

138144
export type { Logger, ComponentLogger }

0 commit comments

Comments
 (0)