Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Add global error handler #98

Merged
merged 3 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-scroll": "^1.7.10",
"react-sound": "^1.2.0",
"rimble-ui": "0.4.2",
"sourcemapped-stacktrace": "^1.1.11",
"styled-components": "^4.1.3",
"web3": "1.0.0-beta.36",
"ya-bbcode": "^1.0.9"
Expand Down
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ class App extends Component {

const { voteStartTime, voteEndTime, trashBox } = this.state;
const web3Props = { plasma: xdaiweb3, web3, account, metaAccount };

return (
<>
{account ? (
Expand Down
2 changes: 1 addition & 1 deletion src/components/Advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class Advanced extends React.Component {
{privateKey &&
<div>
<div className="content ops row settings-row" >
<PrimaryButton onClick={() => this.props.history.push("/burn")}>
<PrimaryButton onClick={() => this.props.history.push("/burn")}>
<Scaler config={{startZoomAt:400,origin:"50% 50%"}}>
<i className="fas fa-fire"/> {i18n.t('burn')}
</Scaler>
Expand Down
82 changes: 82 additions & 0 deletions src/components/ErrorBoundary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Nice stuff here: https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html
// Code taken from Dan Abramov's pen https://codepen.io/gaearon/pen/wqvxGa

import React from "react";
import { Box, Card, Heading, Flex, Button } from "rimble-ui";
import styled from "styled-components";
import { CopyToClipboard } from "react-copy-to-clipboard";
import { mapStackTrace } from "sourcemapped-stacktrace";

const Container = styled(Card)`
margin: 0;
`;

const Details = styled(Box).attrs(() => ({
mt: 0,
mb: 4,
fontSize: 2
}))``;

const Pre = styled.pre`
font-family: monospace;
font-size: 10px;
`;

export default class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}

componentDidCatch(error, errorInfo) {
this.setState({ broken: true });

mapStackTrace(error.stack, stack => {
const message = [
error.toString(),
...stack,
"---",
error.toString(),
errorInfo.componentStack
].join("\n");
this.setState({ message });
});
}

render() {
const { broken, message, copied } = this.state;

if (broken) {
return (
<Container>
<h2>Something went wrong 😢</h2>
<p>
Please let us know about this error. If you did it already, reload
the page, thanks!
</p>

{message ? (
<>
<Details>
<details>
<Pre>{message}</Pre>
</details>
</Details>

<CopyToClipboard
text={message}
onCopy={() => this.setState({ copied: true })}
>
<Button>Copy to clipboard</Button>
</CopyToClipboard>
{copied && <p>Copied!</p>}
</>
) : (
<p>Loading details, wait... ⌛</p>
)}
</Container>
);
}
return this.props.children;
}
}
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { BrowserRouter as Router } from 'react-router-dom';
import i18n from "./i18n";
import { I18nextProvider } from "react-i18next";
import { ThemeProvider } from "rimble-ui";
import ErrorBoundary from "./components/ErrorBoundary";
import theme from "./theme";


ReactDOM.render(
<ThemeProvider theme={theme} style={{ height: '100%' }}>
<I18nextProvider i18n={i18n}>
<Router>
<App />
<ErrorBoundary>
<App />
</ErrorBoundary>
</Router>
</I18nextProvider>
</ThemeProvider>,
Expand Down