-
-
Notifications
You must be signed in to change notification settings - Fork 472
/
getCallFrame.ts
35 lines (28 loc) · 963 Bytes
/
getCallFrame.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
// Ignore the source files traces for local testing.
const SOURCE_FRAME = /[\/\\]msw[\/\\]src[\/\\](.+)/
const BUILD_FRAME =
/(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/
/**
* Return the stack trace frame of a function's invocation.
*/
export function getCallFrame(error: Error) {
// In <IE11, new Error may return an undefined stack
const stack = error.stack
if (!stack) {
return
}
const frames: string[] = stack.split('\n').slice(1)
// Get the first frame that doesn't reference the library's internal trace.
// Assume that frame is the invocation frame.
const declarationFrame = frames.find((frame) => {
return !(SOURCE_FRAME.test(frame) || BUILD_FRAME.test(frame))
})
if (!declarationFrame) {
return
}
// Extract file reference from the stack frame.
const declarationPath = declarationFrame
.replace(/\s*at [^()]*\(([^)]+)\)/, '$1')
.replace(/^@/, '')
return declarationPath
}