-
Notifications
You must be signed in to change notification settings - Fork 24.2k
/
ExceptionsManager.js
242 lines (218 loc) · 7.61 KB
/
ExceptionsManager.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* 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 type {ExceptionData} from './NativeExceptionsManager';
class SyntheticError extends Error {
name: string = '';
}
type ExceptionDecorator = ExceptionData => ExceptionData;
let userExceptionDecorator: ?ExceptionDecorator;
let inUserExceptionDecorator = false;
/**
* Allows the app to add information to the exception report before it is sent
* to native. This API is not final.
*/
function unstable_setExceptionDecorator(
exceptionDecorator: ?ExceptionDecorator,
) {
userExceptionDecorator = exceptionDecorator;
}
function preprocessException(data: ExceptionData): ExceptionData {
if (userExceptionDecorator && !inUserExceptionDecorator) {
inUserExceptionDecorator = true;
try {
return userExceptionDecorator(data);
} catch {
// Fall through
} finally {
inUserExceptionDecorator = false;
}
}
return data;
}
/**
* Handles the developer-visible aspect of errors and exceptions
*/
let exceptionID = 0;
function reportException(
e: ExtendedError,
isFatal: boolean,
reportToConsole: boolean, // only true when coming from handleException; the error has not yet been logged
) {
const parseErrorStack = require('./Devtools/parseErrorStack');
const stack = parseErrorStack(e?.stack);
const currentExceptionID = ++exceptionID;
const originalMessage = e.message || '';
let message = originalMessage;
if (e.componentStack != null) {
message += `\n\nThis error is located at:${e.componentStack}`;
}
const namePrefix = e.name == null || e.name === '' ? '' : `${e.name}: `;
if (!message.startsWith(namePrefix)) {
message = namePrefix + message;
}
message =
e.jsEngine == null ? message : `${message}, js engine: ${e.jsEngine}`;
const data = preprocessException({
message,
originalMessage: message === originalMessage ? null : originalMessage,
name: e.name == null || e.name === '' ? null : e.name,
componentStack:
typeof e.componentStack === 'string' ? e.componentStack : null,
stack,
id: currentExceptionID,
isFatal,
extraData: {
jsEngine: e.jsEngine,
rawStack: e.stack,
},
});
if (reportToConsole) {
// we feed back into console.error, to make sure any methods that are
// monkey patched on top of console.error are called when coming from
// handleException
console.error(data.message);
}
if (__DEV__) {
const LogBox = require('../LogBox/LogBox');
LogBox.addException({
...data,
isComponentError: !!e.isComponentError,
});
} else if (isFatal || e.type !== 'warn') {
const NativeExceptionsManager =
require('./NativeExceptionsManager').default;
if (NativeExceptionsManager) {
NativeExceptionsManager.reportException(data);
}
}
}
declare var console: typeof console & {
_errorOriginal: typeof console.error,
reportErrorsAsExceptions: boolean,
...
};
// If we trigger console.error _from_ handleException,
// we do want to make sure that console.error doesn't trigger error reporting again
let inExceptionHandler = false;
/**
* Logs exceptions to the (native) console and displays them
*/
function handleException(e: mixed, isFatal: boolean) {
let error: Error;
if (e instanceof Error) {
error = e;
} else {
// Workaround for reporting errors caused by `throw 'some string'`
// Unfortunately there is no way to figure out the stacktrace in this
// case, so if you ended up here trying to trace an error, look for
// `throw '<error message>'` somewhere in your codebase.
error = new SyntheticError(e);
}
try {
inExceptionHandler = true;
/* $FlowFixMe[class-object-subtyping] added when improving typing for this
* parameters */
reportException(error, isFatal, /*reportToConsole*/ true);
} finally {
inExceptionHandler = false;
}
}
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function reactConsoleErrorHandler(...args) {
// bubble up to any original handlers
console._errorOriginal(...args);
if (!console.reportErrorsAsExceptions) {
return;
}
if (inExceptionHandler) {
// The fundamental trick here is that are multiple entry point to logging errors:
// (see D19743075 for more background)
//
// 1. An uncaught exception being caught by the global handler
// 2. An error being logged throw console.error
//
// However, console.error is monkey patched multiple times: by this module, and by the
// DevTools setup that sends messages to Metro.
// The patching order cannot be relied upon.
//
// So, some scenarios that are handled by this flag:
//
// Logging an error:
// 1. console.error called from user code
// 2. (possibly) arrives _first_ at DevTool handler, send to Metro
// 3. Bubbles to here
// 4. goes into report Exception.
// 5. should not trigger console.error again, to avoid looping / logging twice
// 6. should still bubble up to original console
// (which might either be console.log, or the DevTools handler in case it patched _earlier_ and (2) didn't happen)
//
// Throwing an uncaught exception:
// 1. exception thrown
// 2. picked up by handleException
// 3. should be send to console.error (not console._errorOriginal, as DevTools might have patched _later_ and it needs to send it to Metro)
// 4. that _might_ bubble again to the `reactConsoleErrorHandle` defined here
// -> should not handle exception _again_, to avoid looping / showing twice (this code branch)
// 5. should still bubble up to original console (which might either be console.log, or the DevTools handler in case that one patched _earlier_)
return;
}
let error;
const firstArg = args[0];
if (firstArg?.stack) {
// reportException will console.error this with high enough fidelity.
error = firstArg;
} else {
const stringifySafe = require('../Utilities/stringifySafe').default;
if (typeof firstArg === 'string' && firstArg.startsWith('Warning: ')) {
// React warnings use console.error so that a stack trace is shown, but
// we don't (currently) want these to show a redbox
// (Note: Logic duplicated in polyfills/console.js.)
return;
}
const message = args
.map(arg => (typeof arg === 'string' ? arg : stringifySafe(arg)))
.join(' ');
error = new SyntheticError(message);
error.name = 'console.error';
}
reportException(
/* $FlowFixMe[class-object-subtyping] added when improving typing for this
* parameters */
error,
false, // isFatal
false, // reportToConsole
);
}
/**
* Shows a redbox with stacktrace for all console.error messages. Disable by
* setting `console.reportErrorsAsExceptions = false;` in your app.
*/
function installConsoleErrorReporter() {
// Enable reportErrorsAsExceptions
if (console._errorOriginal) {
return; // already installed
}
// Flow doesn't like it when you set arbitrary values on a global object
console._errorOriginal = console.error.bind(console);
console.error = reactConsoleErrorHandler;
if (console.reportErrorsAsExceptions === undefined) {
// Individual apps can disable this
// Flow doesn't like it when you set arbitrary values on a global object
console.reportErrorsAsExceptions = true;
}
}
module.exports = {
handleException,
installConsoleErrorReporter,
SyntheticError,
unstable_setExceptionDecorator,
};