Skip to content

Commit 57fa69d

Browse files
committed
Resolve sourcemaps for friendlier traces
1 parent 60d9aa0 commit 57fa69d

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

packages/react-dev-utils/failFast.js

+43-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(function() {
22
const ErrorStackParser = require('error-stack-parser')
3+
const StackTraceGPS = require('stacktrace-gps')
4+
const gps = new StackTraceGPS()
35

46
const overlayStyle = {
57
position: 'fixed',
@@ -82,16 +84,47 @@
8284
}
8385

8486
function crash(error, unhandledRejection = false) {
85-
let frames = []
86-
try {
87-
frames = ErrorStackParser.parse(error)
88-
} catch (e) {
89-
}
90-
if (unhandledRejection) {
91-
render(`Unhandled Rejection (${error.name})`, error.message, frames)
92-
} else {
93-
render(error.name, error.message, frames)
94-
}
87+
new Promise(function(resolve, reject) {
88+
let frames = []
89+
90+
// Wrap all this up to make sure we have a fail case (external apis) ...
91+
try {
92+
// Error -> StackFrame[]
93+
frames = ErrorStackParser.parse(error)
94+
if (frames.length === 0) {
95+
resolve(frames)
96+
return
97+
}
98+
99+
// Resolve StackFrames via sourcemaps and magic
100+
const frames2 = []
101+
let pending = frames.length
102+
frames.forEach(function(frame, index) {
103+
gps.pinpoint(frame).then(function(nFrame) {
104+
frames2[index] = nFrame
105+
if (--pending === 0) resolve(frames2)
106+
}).catch(function() {
107+
// Failed to pinpoint frame ... reuse old frame.
108+
frames2[index] = frame
109+
if (--pending === 0) resolve(frames2)
110+
})
111+
})
112+
} catch (e) {
113+
// Failed to resolve frames at one point or another (synchronous)
114+
// Default to using `frames` which should contain the browser's stack
115+
resolve(frames)
116+
}
117+
}).then(function(frames) {
118+
if (unhandledRejection) {
119+
render(`Unhandled Rejection (${error.name})`, error.message, frames)
120+
} else {
121+
render(error.name, error.message, frames)
122+
}
123+
}).catch(function() {
124+
// This is another fail case (unlikely to happen)
125+
// e.g. render(...) throws an error with provided arguments
126+
render('Error', 'Unknown Error (failure to materialize)', [])
127+
})
95128
}
96129

97130
window.onerror = function(messageOrEvent, source, lineno, colno, error) {

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"html-entities": "1.2.0",
3232
"opn": "4.0.2",
3333
"sockjs-client": "1.0.3",
34+
"stacktrace-gps": "2.4.4",
3435
"strip-ansi": "3.0.1"
3536
}
3637
}

0 commit comments

Comments
 (0)