2018-11-04 16:32:34 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
ErrorCatcher
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
<Components.ErrorCatcher>
|
|
|
|
<YourComponentTree />
|
|
|
|
</Components.ErrorCatcher>
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-01-10 14:23:50 +01:00
|
|
|
import { Components, registerComponent, withCurrentUser, withSiteData } from 'meteor/vulcan:core';
|
2019-01-23 14:29:59 +01:00
|
|
|
import { withRouter } from 'react-router';
|
2018-11-04 16:32:34 +09:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { Errors } from '../modules/errors.js';
|
|
|
|
|
|
|
|
class ErrorCatcher extends Component {
|
|
|
|
state = {
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidCatch = (error, errorInfo) => {
|
2018-12-10 15:49:25 +09:00
|
|
|
const { currentUser, siteData = {} } = this.props;
|
2018-11-24 09:56:41 +09:00
|
|
|
const { sourceVersion } = siteData;
|
2018-11-04 16:32:34 +09:00
|
|
|
this.setState({ error });
|
|
|
|
Errors.log({
|
|
|
|
message: error.message,
|
|
|
|
error,
|
2018-11-24 09:56:41 +09:00
|
|
|
details: { ...errorInfo, sourceVersion },
|
2018-11-04 16:32:34 +09:00
|
|
|
currentUser,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-23 14:29:59 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (
|
|
|
|
this.props.location &&
|
|
|
|
prevProps.location &&
|
|
|
|
this.props.location.pathname &&
|
|
|
|
prevProps.location.pathname &&
|
|
|
|
prevProps.location.pathname !== this.props.location.pathname
|
|
|
|
) {
|
|
|
|
// reset the component state when the route changes to re-render the app and avodi blocking the navigation
|
|
|
|
this.setState({ error: null });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:32:34 +09:00
|
|
|
render() {
|
|
|
|
const { error } = this.state;
|
|
|
|
return error ? (
|
|
|
|
<div className="error-catcher">
|
2019-01-23 14:29:59 +01:00
|
|
|
<Components.Flash
|
|
|
|
message={{ id: 'errors.generic_report', properties: { errorMessage: error.message } }}
|
|
|
|
/>
|
2018-11-04 16:32:34 +09:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
this.props.children
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 14:29:59 +01:00
|
|
|
registerComponent('ErrorCatcher', ErrorCatcher, withCurrentUser, withSiteData, withRouter);
|