I'm trying to capture all errors concurring in react app including event handler errors using window.onerror
the following way in App.js
:
componentWillMount() {
this.startErrorLog();
}
startErrorLog() {
const {history} = this.props;
window.onerror = (message, file, line, column, errorObject) => {
column = column || (window.event && window.event.errorCharacter);
let stack = errorObject ? errorObject.stack : null;
//trying to get stack from IE
if (!stack) {
stack = [];
// eslint-disable-next-line
let f = arguments.callee.caller;
while (f) {
stack.push(f.name);
f = f.caller;
}
errorObject['stack'] = stack;
}
const error = {
message,
fileName: file,
lineNumber: line,
columnNumber: column,
stack
};
if (process.env.NODE_ENV === 'production') {
Sentry.captureException(error);
}
history.push('/error');
return false;
}
}
Inspired from: can you catch all errors of a React.js app with a try/catch block?
My problem is that when an error pops App.js
is not rendered anymore and it seems as if react unmounts everything from the root element and so nothing is being rendered so the App.js
holding the routing is not rendering thus the Error
page is not being rendered.
Any ideas why react unmounts everything?