Skip to content

Commit 8712e51

Browse files
authored
Merge pull request #1220 from nrkno/fix/stringifyError-UserError
fix: make stringifyError handle UserError better (SOFIE-3337)
2 parents 931a430 + 0233073 commit 8712e51

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
2+
import { UserError, UserErrorMessage } from '../error'
3+
4+
describe('UserError', () => {
5+
test('stringifyError', () => {
6+
const rawError = new Error('raw')
7+
rawError.stack = 'mock stack'
8+
const userError = UserError.from(rawError, UserErrorMessage.PartNotFound, { key: 'translatable message' })
9+
10+
expect(stringifyError(userError)).toEqual(
11+
'UserError: ' +
12+
JSON.stringify({
13+
rawError: 'Error: raw, mock stack',
14+
message: {
15+
key: 'The selected part does not exist',
16+
args: {
17+
key: 'translatable message',
18+
},
19+
},
20+
key: 25,
21+
errorCode: 500,
22+
})
23+
)
24+
25+
// serialized and restored
26+
const restored = JSON.parse(userError.toString())
27+
expect(stringifyError(restored)).toEqual('raw, mock stack')
28+
})
29+
})

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +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-
str = ''
15-
if ((error as Error).message) str += `${(error as Error).message} ` // Is an Error
16-
if ((error as any).reason) str += `${(error as any).reason} ` // Is a Meteor.Error
17-
if ((error as any).details) str += `${(error as any).details} `
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(', ')
1821
}
1922

2023
if (!str) {
@@ -34,7 +37,7 @@ export function stringifyError(error: unknown, noStack = false): string {
3437
}
3538

3639
if (!noStack) {
37-
if (error && typeof error === 'object' && (error as any).stack) {
40+
if (error && typeof error === 'object' && typeof (error as any).stack === 'string') {
3841
str += ', ' + (error as any).stack
3942
}
4043
}
@@ -43,3 +46,15 @@ export function stringifyError(error: unknown, noStack = false): string {
4346

4447
return str
4548
}
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)