-
Notifications
You must be signed in to change notification settings - Fork 27
/
setup-global-error-listener.js
33 lines (30 loc) 路 1.33 KB
/
setup-global-error-listener.js
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
import { showUnexpectedErrorNotification } from '@commercetools-frontend/actions-global';
import { boot as bootSentry } from '@commercetools-local/sentry';
// Ensure to initialize Sentry as soon as possible, so that we have the chance
// of catching possible errors.
bootSentry();
export default function setupGlobalErrorListener(dispatch) {
// Capture unhandled errors generated from rejected Promises.
//
// http://www.2ality.com/2016/04/unhandled-rejections.html
// https://docs.getsentry.com/hosted/clients/javascript/usage/#promises
//
// Note: this currently works only in Chrome, and it might not be needed.
// We just keep it here as a nice-to-have thing.
window.addEventListener('unhandledrejection', event => {
if (process.env.NODE_ENV !== 'production')
// eslint-disable-next-line no-console
console.warn(
'An uncaught promise has been rejected and not properly ' +
'handled. This is most likely a bug in the software. Please ensure ' +
'that the promise is correctly handled.'
);
dispatch(
showUnexpectedErrorNotification({ error: { message: event.reason } })
);
});
// Capture normal global errors coming from non Promise code.
window.addEventListener('error', errorEvent => {
dispatch(showUnexpectedErrorNotification({ error: errorEvent }));
});
}