Skip to content

Commit 0233073

Browse files
committed
fix: further improve stringifyError
1 parent e2ecc7e commit 0233073

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

packages/shared-lib/src/lib/stringifyError.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export function stringifyError(error: unknown, noStack = false): string {
1111
// Has a custom toString() method
1212
str = `${(error as any).toString()}`
1313
} else {
14-
const strings: string[] = []
15-
if (typeof (error as any).rawError === 'string') strings.push(`${(error as any).rawError}`) // Is an UserError
16-
if (typeof (error as Error).message === 'string') strings.push(`${(error as Error).message}`) // Is an Error
17-
if (typeof (error as any).reason === 'string') strings.push(`${(error as any).reason}`) // Is a Meteor.Error
18-
if (typeof (error as any).details === 'string') strings.push(` ${(error as any).details}`)
19-
20-
str = strings.join(', ')
14+
const strings: (string | undefined)[] = [
15+
stringify((error as any).rawError), // UserError
16+
stringify((error as Error).message), // Error
17+
stringify((error as any).reason), // Meteor.Error
18+
stringify((error as any).details),
19+
]
20+
str = strings.filter(Boolean).join(', ')
2121
}
2222

2323
if (!str) {
@@ -46,3 +46,15 @@ export function stringifyError(error: unknown, noStack = false): string {
4646

4747
return str
4848
}
49+
50+
function stringify(v: any): string | undefined {
51+
// Tries to stringify objects if they have a toString() that returns something sensible
52+
if (v === undefined) return undefined
53+
if (v === null) return 'null'
54+
55+
if (typeof v === 'object') {
56+
const str = `${v}`
57+
if (str !== '[object Object]') return str
58+
return undefined
59+
} else return `${v}`
60+
}

0 commit comments

Comments
 (0)