forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportGlobalError.js
51 lines (49 loc) · 1.57 KB
/
reportGlobalError.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
const reportGlobalError: (error: mixed) => void =
typeof reportError === 'function'
? // In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
reportError
: error => {
if (
typeof window === 'object' &&
typeof window.ErrorEvent === 'function'
) {
// Browser Polyfill
const message =
typeof error === 'object' &&
error !== null &&
typeof error.message === 'string'
? // eslint-disable-next-line react-internal/safe-string-coercion
String(error.message)
: // eslint-disable-next-line react-internal/safe-string-coercion
String(error);
const event = new window.ErrorEvent('error', {
bubbles: true,
cancelable: true,
message: message,
error: error,
});
const shouldLog = window.dispatchEvent(event);
if (!shouldLog) {
return;
}
} else if (
typeof process === 'object' &&
// $FlowFixMe[method-unbinding]
typeof process.emit === 'function'
) {
// Node Polyfill
process.emit('uncaughtException', error);
return;
}
console['error'](error);
};
export default reportGlobalError;