Skip to content

Commit 36ce578

Browse files
hipstersmoothieTimer
authored andcommitted
Expose reportRuntimeError (#4709)
* factor out crashWithFrames and expose reportRuntimeError * address code review and move error call to handleRuntimeError
1 parent 1a8003d commit 36ce578

File tree

2 files changed

+64
-40
lines changed

2 files changed

+64
-40
lines changed

Diff for: packages/react-error-overlay/src/index.js

+38-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
*/
77

88
/* @flow */
9-
import { listenToRuntimeErrors } from './listenToRuntimeErrors';
9+
import {
10+
listenToRuntimeErrors,
11+
crashWithFrames,
12+
} from './listenToRuntimeErrors';
1013
import { iframeStyle } from './styles';
1114
import { applyStyles } from './utils/dom/css';
1215

@@ -47,6 +50,14 @@ export function reportBuildError(error: string) {
4750
update();
4851
}
4952

53+
export function reportRuntimeError(
54+
error: Error,
55+
options?: RuntimeReportingOption = {}
56+
) {
57+
currentRuntimeErrorOptions = options;
58+
crashWithFrames(handleRuntimeError(options))(error);
59+
}
60+
5061
export function dismissBuildError() {
5162
currentBuildError = null;
5263
update();
@@ -64,28 +75,35 @@ export function startReportingRuntimeErrors(options: RuntimeReportingOptions) {
6475
);
6576
}
6677
currentRuntimeErrorOptions = options;
67-
stopListeningToRuntimeErrors = listenToRuntimeErrors(errorRecord => {
68-
try {
69-
if (typeof options.onError === 'function') {
70-
options.onError.call(null);
71-
}
72-
} finally {
73-
handleRuntimeError(errorRecord);
74-
}
75-
}, options.filename);
78+
stopListeningToRuntimeErrors = listenToRuntimeErrors(
79+
handleRuntimeError(options),
80+
options.filename
81+
);
7682
}
7783

78-
function handleRuntimeError(errorRecord) {
79-
if (
80-
currentRuntimeErrorRecords.some(({ error }) => error === errorRecord.error)
81-
) {
82-
// Deduplicate identical errors.
83-
// This fixes https://github.com/facebook/create-react-app/issues/3011.
84-
return;
84+
const handleRuntimeError = (options: RuntimeReportingOptions) => (
85+
errorRecord: ErrorRecord
86+
) => {
87+
try {
88+
if (typeof options.onError === 'function') {
89+
options.onError.call(null);
90+
}
91+
} finally {
92+
if (
93+
currentRuntimeErrorRecords.some(
94+
({ error }) => error === errorRecord.error
95+
)
96+
) {
97+
// Deduplicate identical errors.
98+
// This fixes https://github.com/facebook/create-react-app/issues/3011.
99+
return;
100+
}
101+
currentRuntimeErrorRecords = currentRuntimeErrorRecords.concat([
102+
errorRecord,
103+
]);
104+
update();
85105
}
86-
currentRuntimeErrorRecords = currentRuntimeErrorRecords.concat([errorRecord]);
87-
update();
88-
}
106+
};
89107

90108
export function dismissRuntimeErrors() {
91109
currentRuntimeErrorRecords = [];

Diff for: packages/react-error-overlay/src/listenToRuntimeErrors.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,40 @@ export type ErrorRecord = {|
3737
stackFrames: StackFrame[],
3838
|};
3939

40+
export const crashWithFrames = (crash: ErrorRecord => void) => (
41+
error: Error,
42+
unhandledRejection = false
43+
) => {
44+
getStackFrames(error, unhandledRejection, CONTEXT_SIZE)
45+
.then(stackFrames => {
46+
if (stackFrames == null) {
47+
return;
48+
}
49+
crash({
50+
error,
51+
unhandledRejection,
52+
contextSize: CONTEXT_SIZE,
53+
stackFrames,
54+
});
55+
})
56+
.catch(e => {
57+
console.log('Could not get the stack frames of error:', e);
58+
});
59+
};
60+
4061
export function listenToRuntimeErrors(
4162
crash: ErrorRecord => void,
4263
filename: string = '/static/js/bundle.js'
4364
) {
44-
function crashWithFrames(error: Error, unhandledRejection = false) {
45-
getStackFrames(error, unhandledRejection, CONTEXT_SIZE)
46-
.then(stackFrames => {
47-
if (stackFrames == null) {
48-
return;
49-
}
50-
crash({
51-
error,
52-
unhandledRejection,
53-
contextSize: CONTEXT_SIZE,
54-
stackFrames,
55-
});
56-
})
57-
.catch(e => {
58-
console.log('Could not get the stack frames of error:', e);
59-
});
60-
}
61-
registerError(window, error => crashWithFrames(error, false));
62-
registerPromise(window, error => crashWithFrames(error, true));
65+
const crashWithFramesRunTime = crashWithFrames(crash);
66+
67+
registerError(window, error => crashWithFramesRunTime(error, false));
68+
registerPromise(window, error => crashWithFramesRunTime(error, true));
6369
registerStackTraceLimit();
6470
registerReactStack();
6571
permanentRegisterConsole('error', (warning, stack) => {
6672
const data = massageWarning(warning, stack);
67-
crashWithFrames(
73+
crashWithFramesRunTime(
6874
// $FlowFixMe
6975
{
7076
message: data.message,

0 commit comments

Comments
 (0)