/
ErrorHandlers.js
116 lines (103 loc) · 3.7 KB
/
ErrorHandlers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* 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.
*
* @format
* @flow strict
*/
'use strict';
import type {ExtendedError} from './ExtendedError';
import {SyntheticError, handleException} from './ExceptionsManager';
type ErrorInfo = {
+componentStack?: ?string,
// $FlowFixMe[unclear-type] unknown props and state.
+errorBoundary?: ?React$Component<any, any>,
};
export function onUncaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
let error;
// Typically, `errorValue` should be an error. However, other values such as
// strings (or even null) are sometimes thrown.
if (errorValue instanceof Error) {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (errorValue: ExtendedError);
} else if (typeof errorValue === 'string') {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError(errorValue): ExtendedError);
} else {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError('Unspecified error'): ExtendedError);
}
try {
// $FlowFixMe[incompatible-use] this is in try/catch.
error.componentStack = errorInfo.componentStack;
error.isComponentError = true;
} catch {
// Ignored.
}
// Uncaught errors are fatal.
handleException(error, true);
}
export function onCaughtError(errorValue: mixed, errorInfo: ErrorInfo): void {
let error;
// Typically, `errorValue` should be an error. However, other values such as
// strings (or even null) are sometimes thrown.
if (errorValue instanceof Error) {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (errorValue: ExtendedError);
} else if (typeof errorValue === 'string') {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError(errorValue): ExtendedError);
} else {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError('Unspecified error'): ExtendedError);
}
try {
// $FlowFixMe[incompatible-use] this is in try/catch.
error.componentStack = errorInfo.componentStack;
error.isComponentError = true;
} catch {
// Ignored.
}
// Caught errors are not fatal.
handleException(error, false);
}
export function onRecoverableError(
errorValue: mixed,
errorInfo: ErrorInfo,
): void {
let error;
// Typically, `errorValue` should be an error. However, other values such as
// strings (or even null) are sometimes thrown.
if (errorValue instanceof Error) {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (errorValue: ExtendedError);
} else if (typeof errorValue === 'string') {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError(errorValue): ExtendedError);
} else {
/* $FlowFixMe[class-object-subtyping] added when improving typing for
* this parameters */
error = (new SyntheticError('Unspecified error'): ExtendedError);
}
try {
// $FlowFixMe[incompatible-use] this is in try/catch.
error.componentStack = errorInfo.componentStack;
error.isComponentError = true;
} catch {
// Ignored.
}
// Recoverable errors should only be warnings.
// This will make it a soft error in LogBox.
// TODO: improve the logging for recoverable errors in prod.
console.warn(error);
}