|
1 | 1 | (function() {
|
2 | 2 | const ErrorStackParser = require('error-stack-parser')
|
| 3 | + const StackTraceGPS = require('stacktrace-gps') |
| 4 | + const gps = new StackTraceGPS() |
3 | 5 |
|
4 | 6 | const overlayStyle = {
|
5 | 7 | position: 'fixed',
|
|
82 | 84 | }
|
83 | 85 |
|
84 | 86 | 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 | + }) |
95 | 128 | }
|
96 | 129 |
|
97 | 130 | window.onerror = function(messageOrEvent, source, lineno, colno, error) {
|
|
0 commit comments