lbry-desktop/ui/component/errorBoundary/view.jsx

99 lines
2.6 KiB
React
Raw Normal View History

// @flow
2019-08-13 07:35:13 +02:00
import type { Node } from 'react';
2019-03-15 17:37:56 +01:00
import { Lbryio } from 'lbryinc';
2019-08-13 07:35:13 +02:00
import React, { Fragment } from 'react';
import Yrbl from 'component/yrbl';
import Button from 'component/button';
import { withRouter } from 'react-router';
2019-05-20 18:02:22 +02:00
import Native from 'native';
2019-06-04 21:00:33 +02:00
import { Lbry } from 'lbry-redux';
type Props = {
2019-08-13 07:35:13 +02:00
children: Node,
history: {
replace: string => void,
},
};
type State = {
hasError: boolean,
};
class ErrorBoundary extends React.Component<Props, State> {
constructor() {
super();
this.state = { hasError: false };
(this: any).refresh = this.refresh.bind(this);
}
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error: { stack: string }) {
2019-06-04 21:00:33 +02:00
let errorMessage = 'Uncaught error\n';
2019-05-20 18:02:22 +02:00
// @if TARGET='web'
2019-06-04 21:00:33 +02:00
errorMessage += 'lbry.tv\n';
2019-12-06 16:30:46 +01:00
errorMessage += `page: ${window.location.pathname + window.location.search}\n`;
errorMessage += error.stack;
2019-05-20 18:02:22 +02:00
this.log(errorMessage);
// @endif
// @if TARGET='app'
Native.getAppVersionInfo().then(({ localVersion }) => {
2019-06-04 21:00:33 +02:00
Lbry.version().then(({ lbrynet_version: sdkVersion }) => {
errorMessage += `app version: ${localVersion}\n`;
errorMessage += `sdk version: ${sdkVersion}\n`;
errorMessage += `page: ${window.location.href.split('.html')[1]}\n`;
errorMessage += `${error.stack}`;
this.log(errorMessage);
});
2019-05-20 18:02:22 +02:00
});
// @endif
}
2019-05-20 18:02:22 +02:00
log(message) {
declare var app: { env: string };
2019-06-05 07:15:43 +02:00
if (app.env === 'production') {
Lbryio.call('event', 'desktop_error', { error_message: message });
}
}
refresh() {
const { history } = this.props;
// use history.replace instead of history.push so the user can't click back to the errored page
history.replace('');
this.setState({ hasError: false });
}
render() {
2019-03-08 20:20:17 +01:00
if (this.state.hasError) {
return (
<div className="main main--empty">
<Yrbl
type="sad"
title={__('Aw shucks!')}
subtitle={
2019-08-13 07:35:13 +02:00
<Fragment>
2019-10-01 06:53:33 +02:00
{__("There was an error. It's been reported and will be fixed")}. {__('Try')}{' '}
<Button
button="link"
className="load-screen__button"
label={__('refreshing the app')}
onClick={this.refresh}
/>{' '}
{__("to fix it. If that doesn't work, press CMD/CTRL-R to reset to the homepage.")}.
2019-08-13 07:35:13 +02:00
</Fragment>
}
/>
</div>
);
}
return this.props.children;
}
}
export default withRouter(ErrorBoundary);