Skip to content

Commit 0dba0af

Browse files
yungstersfacebook-github-bot
authored andcommitted
ExceptionsManager: Minor Code Cleanup
Summary: Cleans up `reactConsoleErrorHandler` in `ExceptionsManager` using modern language features, and fixes a minor edge case with how warning-like errors are handled. Changelog: [General][Fixed] - Avoid downgrading `console.error` when passed warning-like objects. Reviewed By: GijsWeterings Differential Revision: D28418488 fbshipit-source-id: 394e8608c2c81c794c9a0fc155142dcfcfe1c661
1 parent eeb8e58 commit 0dba0af

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

Libraries/Core/ExceptionsManager.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ function handleException(e: mixed, isFatal: boolean) {
175175
}
176176
}
177177

178-
function reactConsoleErrorHandler() {
178+
function reactConsoleErrorHandler(...args) {
179179
// bubble up to any original handlers
180-
console._errorOriginal.apply(console, arguments);
180+
console._errorOriginal(...args);
181181
if (!console.reportErrorsAsExceptions) {
182182
return;
183183
}
@@ -213,31 +213,33 @@ function reactConsoleErrorHandler() {
213213
return;
214214
}
215215

216-
if (arguments[0] && arguments[0].stack) {
216+
let error;
217+
218+
const firstArg = args[0];
219+
if (firstArg?.stack) {
217220
// reportException will console.error this with high enough fidelity.
218-
reportException(
219-
arguments[0],
220-
/* isFatal */ false,
221-
/*reportToConsole*/ false,
222-
);
221+
error = firstArg;
223222
} else {
224223
const stringifySafe = require('../Utilities/stringifySafe').default;
225-
const str = Array.prototype.map
226-
.call(arguments, value =>
227-
typeof value === 'string' ? value : stringifySafe(value),
228-
)
229-
.join(' ');
230-
231-
if (str.slice(0, 9) === 'Warning: ') {
224+
if (typeof firstArg === 'string' && firstArg.startsWith('Warning: ')) {
232225
// React warnings use console.error so that a stack trace is shown, but
233226
// we don't (currently) want these to show a redbox
234227
// (Note: Logic duplicated in polyfills/console.js.)
235228
return;
236229
}
237-
const error: ExtendedError = new SyntheticError(str);
230+
const message = args
231+
.map(arg => (typeof arg === 'string' ? arg : stringifySafe(arg)))
232+
.join(' ');
233+
234+
error = new SyntheticError(message);
238235
error.name = 'console.error';
239-
reportException(error, /* isFatal */ false, /*reportToConsole*/ false);
240236
}
237+
238+
reportException(
239+
error,
240+
false, // isFatal
241+
false, // reportToConsole
242+
);
241243
}
242244

243245
/**

Libraries/Core/__tests__/ExceptionsManager-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,18 @@ describe('ExceptionsManager', () => {
358358
expect(mockError.mock.calls[0]).toEqual(args);
359359
});
360360

361+
test('logging a warning-looking object', () => {
362+
// Forces `strignifySafe` to invoke `toString()`.
363+
const object = {toString: () => 'Warning: Some error may have happened'};
364+
object.cycle = object;
365+
366+
const args = [object];
367+
368+
console.error(...args);
369+
370+
expect(nativeReportException).toHaveBeenCalled();
371+
});
372+
361373
test('reportErrorsAsExceptions = false', () => {
362374
console.reportErrorsAsExceptions = false;
363375
const message = 'Some error happened';

0 commit comments

Comments
 (0)