main
js 51 lines 1.57 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 const reportGlobalError: (error: mixed) => void =
11 typeof reportError === 'function'
12 ? // In modern browsers, reportError will dispatch an error event,
13 // emulating an uncaught JavaScript error.
14 reportError
15 : error => {
16 if (
17 typeof window === 'object' &&
18 typeof window.ErrorEvent === 'function'
19 ) {
20 // Browser Polyfill
21 const message =
22 typeof error === 'object' &&
23 error !== null &&
24 typeof error.message === 'string'
25 ? // eslint-disable-next-line react-internal/safe-string-coercion
26 String(error.message)
27 : // eslint-disable-next-line react-internal/safe-string-coercion
28 String(error);
29 const event = new window.ErrorEvent('error', {
30 bubbles: true,
31 cancelable: true,
32 message: message,
33 error: error,
34 });
35 const shouldLog = window.dispatchEvent(event);
36 if (!shouldLog) {
37 return;
38 }
39 } else if (
40 typeof process === 'object' &&
41 // $FlowFixMe[method-unbinding]
42 typeof process.emit === 'function'
43 ) {
44 // Node Polyfill
45 process.emit('uncaughtException', error);
46 return;
47 }
48 console['error'](error);
49 };
50
51 export default reportGlobalError;