Skip to content

Commit ebc89bf

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Cleanup ReactFiberErrorDialog
Summary: Minor cleanup of `ReactFiberErrorDialog` with no behavior changes. Changelog: [Internal] Reviewed By: GijsWeterings Differential Revision: D28423265 fbshipit-source-id: 796777ca2c26d6d7cfae956299786151c74e937f
1 parent 0b37130 commit ebc89bf

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

Libraries/Core/ReactFiberErrorDialog.js

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,49 @@
88
* @flow strict-local
99
*/
1010

11+
import {handleException, SyntheticError} from './ExceptionsManager';
12+
13+
import type {ExtendedError} from './Devtools/parseErrorStack';
14+
1115
export type CapturedError = {
1216
+componentStack: string,
1317
+error: mixed,
1418
+errorBoundary: ?{...},
1519
...
1620
};
1721

18-
import type {ExtendedError} from './Devtools/parseErrorStack';
19-
20-
import {handleException, SyntheticError} from './ExceptionsManager';
22+
const ReactFiberErrorDialog = {
23+
/**
24+
* Intercept lifecycle errors and ensure they are shown with the correct stack
25+
* trace within the native redbox component.
26+
*/
27+
showErrorDialog({componentStack, error: errorValue}: CapturedError): boolean {
28+
let error: ?ExtendedError;
29+
30+
// Typically, `errorValue` should be an error. However, other values such as
31+
// strings (or even null) are sometimes thrown.
32+
if (errorValue instanceof Error) {
33+
error = (errorValue: ExtendedError);
34+
} else if (typeof errorValue === 'string') {
35+
error = (new SyntheticError(errorValue): ExtendedError);
36+
} else {
37+
error = (new SyntheticError('Unspecified error'): ExtendedError);
38+
}
39+
try {
40+
error.componentStack = componentStack;
41+
error.isComponentError = true;
42+
} catch {
43+
// Ignored.
44+
}
45+
46+
handleException(error, false);
47+
48+
// Return false here to prevent ReactFiberErrorLogger default behavior of
49+
// logging error details to console.error. Calls to console.error are
50+
// automatically routed to the native redbox controller, which we've already
51+
// done above by calling ExceptionsManager.
52+
return false;
53+
},
54+
};
2155

22-
/**
23-
* Intercept lifecycle errors and ensure they are shown with the correct stack
24-
* trace within the native redbox component.
25-
*/
26-
function showErrorDialog(capturedError: CapturedError): boolean {
27-
const {componentStack, error} = capturedError;
28-
29-
let errorToHandle;
30-
31-
// Typically Errors are thrown but eg strings or null can be thrown as well.
32-
if (error instanceof Error) {
33-
errorToHandle = (error: ExtendedError);
34-
} else if (typeof error === 'string') {
35-
errorToHandle = (new SyntheticError(error): ExtendedError);
36-
} else {
37-
errorToHandle = (new SyntheticError('Unspecified error'): ExtendedError);
38-
}
39-
try {
40-
errorToHandle.componentStack = componentStack;
41-
errorToHandle.isComponentError = true;
42-
} catch (e) {}
43-
handleException(errorToHandle, false);
44-
45-
// Return false here to prevent ReactFiberErrorLogger default behavior of
46-
// logging error details to console.error. Calls to console.error are
47-
// automatically routed to the native redbox controller, which we've already
48-
// done above by calling ExceptionsManager.
49-
return false;
50-
}
51-
52-
module.exports = {showErrorDialog};
56+
export default ReactFiberErrorDialog;

Libraries/Core/__tests__/ExceptionsManager-test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
'use strict';
1212

13+
const ExceptionsManager = require('../ExceptionsManager');
14+
const NativeExceptionsManager = require('../NativeExceptionsManager').default;
15+
const ReactFiberErrorDialog = require('../ReactFiberErrorDialog').default;
1316
const fs = require('fs');
1417
const path = require('path');
1518

@@ -23,10 +26,8 @@ const capturedErrorDefaults = {
2326
};
2427

2528
describe('ExceptionsManager', () => {
26-
let ReactFiberErrorDialog,
27-
ExceptionsManager,
28-
NativeExceptionsManager,
29-
nativeReportException;
29+
let nativeReportException;
30+
3031
beforeEach(() => {
3132
jest.resetModules();
3233
jest.mock('../NativeExceptionsManager', () => {
@@ -46,11 +47,8 @@ describe('ExceptionsManager', () => {
4647
return {stack};
4748
},
4849
);
49-
jest.spyOn(console, 'error').mockImplementation(() => {});
50-
ReactFiberErrorDialog = require('../ReactFiberErrorDialog');
51-
NativeExceptionsManager = require('../NativeExceptionsManager').default;
50+
jest.spyOn(console, 'error').mockReturnValue(undefined);
5251
nativeReportException = NativeExceptionsManager.reportException;
53-
ExceptionsManager = require('../ExceptionsManager');
5452
});
5553

5654
afterEach(() => {

Libraries/ReactPrivate/ReactNativePrivateInterface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = {
5858
return require('../StyleSheet/flattenStyle');
5959
},
6060
get ReactFiberErrorDialog(): ReactFiberErrorDialog {
61-
return require('../Core/ReactFiberErrorDialog');
61+
return require('../Core/ReactFiberErrorDialog').default;
6262
},
6363
get legacySendAccessibilityEvent(): legacySendAccessibilityEvent {
6464
return require('../Components/AccessibilityInfo/legacySendAccessibilityEvent');

0 commit comments

Comments
 (0)