Updated ErrorHandler so that it logs errors in production

This commit is contained in:
Alejandro Celaya 2019-03-05 14:04:52 +01:00
parent 43097b93e5
commit 701c143149
3 changed files with 11 additions and 3 deletions

View file

@ -3,7 +3,7 @@ import * as PropTypes from 'prop-types';
import './ErrorHandler.scss';
import { Button } from 'reactstrap';
const ErrorHandler = ({ location }) => class ErrorHandler extends React.Component {
const ErrorHandler = ({ location }, { error }) => class ErrorHandler extends React.Component {
static propTypes = {
children: PropTypes.node.isRequired,
};
@ -17,6 +17,12 @@ const ErrorHandler = ({ location }) => class ErrorHandler extends React.Componen
return { hasError: true };
}
componentDidCatch(e) {
if (process.env.NODE_ENV !== 'development') {
error(e);
}
}
render() {
if (this.state.hasError) {
return (

View file

@ -7,6 +7,7 @@ import ErrorHandler from '../ErrorHandler';
const provideServices = (bottle, connect, withRouter) => {
bottle.constant('window', global.window);
bottle.constant('console', global.console);
bottle.serviceFactory('ScrollToTop', ScrollToTop, 'window');
bottle.decorator('ScrollToTop', withRouter);
@ -31,7 +32,7 @@ const provideServices = (bottle, connect, withRouter) => {
bottle.serviceFactory('AsideMenu', AsideMenu, 'DeleteServerButton');
bottle.serviceFactory('ErrorHandler', ErrorHandler, 'window');
bottle.serviceFactory('ErrorHandler', ErrorHandler, 'window', 'console');
};
export default provideServices;

View file

@ -9,10 +9,11 @@ describe('<ErrorHandler />', () => {
reload: jest.fn(),
},
};
const console = { error: jest.fn() };
let wrapper;
beforeEach(() => {
const ErrorHandler = createErrorHandler(window);
const ErrorHandler = createErrorHandler(window, console);
wrapper = shallow(<ErrorHandler children={<span>Foo</span>} />);
});