This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
forked from social-dist0rtion-protocol/planet-a
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add global error handler Fix #91 * Add loading message * oopsy
- Loading branch information
1 parent
50c622c
commit a5c84f0
Showing
6 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters