-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat-error.function.ts
More file actions
72 lines (64 loc) · 1.67 KB
/
format-error.function.ts
File metadata and controls
72 lines (64 loc) · 1.67 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
export interface ErrorAttributes {
[key: string]: unknown
name: string
location: string
message: string
stack?: string
cause?: unknown
}
/**
* Format an error into a loggable object.
*
* @example
* ```json
* {
* "name": "Error",
* "location": "file.js:1",
* "message": "An error occurred",
* "stack": "Error: An error occurred\n at file.js:1\n at file.js:2\n at file.js:3",
* "cause": {
* "name": "OtherError",
* "location": "file.js:2",
* "message": "Another error occurred",
* "stack": "Error: Another error occurred\n at file.js:2\n at file.js:3\n at file.js:4"
* }
* }
* ```
*
* @param error - Error to format
*/
export function formatError(error: Error): ErrorAttributes {
const { name, message, stack, cause, ...errorAttributes } = error
const formattedError: ErrorAttributes = {
name,
location: getCodeLocation(error.stack),
message,
stack,
cause: cause instanceof Error ? formatError(cause) : cause,
}
for (const key in error) {
if (typeof key === 'string' && !['name', 'message', 'stack', 'cause'].includes(key)) {
formattedError[key] = (errorAttributes as Record<string, unknown>)[key]
}
}
return formattedError
}
/**
* Get the location of an error from a stack trace.
*
* @param stack - stack trace to parse
*/
function getCodeLocation(stack?: string): string {
if (!stack) {
return ''
}
const stackLines = stack.split('\n')
const regex = /\(([^()]*?):(\d+?):(\d+?)\)\\?$/
for (const item of stackLines) {
const match = regex.exec(item)
if (Array.isArray(match)) {
return `${match[1]}:${Number(match[2])}`
}
}
return ''
}