-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
46 lines (41 loc) · 1.57 KB
/
index.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
export default function catchErrors({ filename, components, imports }) {
const [React, ErrorReporter, reporterOptions] = imports;
if (!React || !React.Component) {
throw new Error('imports[0] for react-transform-catch-errors does not look like React.');
}
if (typeof ErrorReporter !== 'function') {
throw new Error('imports[1] for react-transform-catch-errors does not look like a React component.');
}
return function wrapToCatchErrors(ReactClass, componentId) {
const originalRender = ReactClass.prototype.render;
Object.defineProperty(ReactClass.prototype, 'render', {
configurable: true,
value: function tryRender() {
try {
return originalRender.apply(this, arguments);
} catch (err) {
setTimeout(() => {
if (typeof console.reportErrorsAsExceptions !== 'undefined') {
let prevReportErrorAsExceptions = console.reportErrorsAsExceptions;
// We're in React Native. Don't throw.
// Stop react-native from triggering its own error handler
console.reportErrorsAsExceptions = false;
// Log an error
console.error(err);
// Reactivate it so other errors are still handled
console.reportErrorsAsExceptions = prevReportErrorAsExceptions;
} else {
throw err;
}
});
return React.createElement(ErrorReporter, {
error: err,
filename,
...reporterOptions
});
}
}
});
return ReactClass;
};
}