From 701c14314927a85e186e35b2f1dcc798703a36a6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Mar 2019 14:04:52 +0100 Subject: [PATCH] Updated ErrorHandler so that it logs errors in production --- src/common/ErrorHandler.js | 8 +++++++- src/common/services/provideServices.js | 3 ++- test/common/ErrorHandler.test.js | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/common/ErrorHandler.js b/src/common/ErrorHandler.js index fb641a79..6dc0af26 100644 --- a/src/common/ErrorHandler.js +++ b/src/common/ErrorHandler.js @@ -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 ( diff --git a/src/common/services/provideServices.js b/src/common/services/provideServices.js index 6fcc775b..f8f5c980 100644 --- a/src/common/services/provideServices.js +++ b/src/common/services/provideServices.js @@ -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; diff --git a/test/common/ErrorHandler.test.js b/test/common/ErrorHandler.test.js index 58325a65..721d7a0d 100644 --- a/test/common/ErrorHandler.test.js +++ b/test/common/ErrorHandler.test.js @@ -9,10 +9,11 @@ describe('', () => { reload: jest.fn(), }, }; + const console = { error: jest.fn() }; let wrapper; beforeEach(() => { - const ErrorHandler = createErrorHandler(window); + const ErrorHandler = createErrorHandler(window, console); wrapper = shallow(Foo} />); });