Skip to content

Commit c742848

Browse files
committed
Add Chrome unhandledrejection listener
1 parent 2945385 commit c742848

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

packages/react-dev-utils/failFast.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@
6767
overlayReference = null
6868
}
6969

70-
function crash(error) {
70+
function crash(error, unhandledRejection = false) {
7171
let frames = []
7272
try {
7373
frames = ErrorStackParser.parse(error)
7474
} catch (e) {
7575
}
76-
render(error.name, error.message, frames)
76+
if (unhandledRejection) {
77+
render(`Unhandled Rejection (${error.name})`, error.message, frames)
78+
} else {
79+
render(error.name, error.message, frames)
80+
}
7781
}
7882

7983
window.onerror = function(messageOrEvent, source, lineno, colno, error) {
@@ -84,9 +88,25 @@
8488
}
8589
}
8690

91+
let promiseHandler = function(event) {
92+
if (event != null && event.reason != null) {
93+
const { reason } = event
94+
if (reason == null || !(reason instanceof Error)) {
95+
crash(new Error(reason), true)
96+
} else {
97+
crash(reason, true)
98+
}
99+
} else {
100+
crash(new Error('Unknown event'), true)
101+
}
102+
}
103+
104+
window.addEventListener('unhandledrejection', promiseHandler)
105+
87106
if (module.hot) {
88107
module.hot.dispose(function() {
89108
unmount()
109+
window.removeEventListener('unhandledrejection', promiseHandler)
90110
})
91111
}
92112
})()

0 commit comments

Comments
 (0)