-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseError.ts
55 lines (51 loc) · 1.16 KB
/
parseError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {isObject} from './isObject'
import {parseStack} from './parseStack'
import {isNodeJS} from './isNodeJS'
export type ParsedError = { message: string, stack: string[], cwd: string, params: any }
const cwd = isNodeJS() ? process.cwd() : ''
export function parseError(e: Error): ParsedError {
if (!e.stack) {
if (typeof e === 'string') {
return {
message: e,
stack: [],
cwd,
params: undefined,
}
}
if (e instanceof Error) {
return {
message: e.message,
stack: [],
cwd,
// @ts-ignore
params: isObject(e.params) ? e.params : undefined
}
}
try {
return {
// @ts-ignore
message: isObject(e) ? '' : JSON.stringify(e),
stack: [],
cwd,
// @ts-ignore
params: isObject(e) ? e : undefined
}
} catch (e) {
return {
message: '',
stack: [],
cwd,
// @ts-ignore
params: isObject(e) ? e : undefined
}
}
}
return {
message: e.message,
stack: parseStack(e.stack),
cwd,
// @ts-ignore
params: isObject(e.params) ? e.params : undefined
}
}