-
Notifications
You must be signed in to change notification settings - Fork 987
feat: added error boundary #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2d6531b
59be45a
dfa43fb
d12d8fc
b9fce75
0cf36a4
a5f1aa9
bea96b4
dbb7913
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import React from "react" | |
import { BrowserRouter as Router } from "react-router-dom" | ||
import { SWRConfig } from "swr" | ||
import { AppRouter } from "./AppRouter" | ||
import { ErrorBoundary } from "./components/ErrorBoundary/ErrorBoundary" | ||
import { GlobalSnackbar } from "./components/GlobalSnackbar/GlobalSnackbar" | ||
import { dark } from "./theme" | ||
import "./theme/globalFonts" | ||
|
@@ -30,13 +31,15 @@ export const App: React.FC = () => { | |
}, | ||
}} | ||
> | ||
<XServiceProvider> | ||
<ThemeProvider theme={dark}> | ||
<CssBaseline /> | ||
<AppRouter /> | ||
<GlobalSnackbar /> | ||
</ThemeProvider> | ||
</XServiceProvider> | ||
<ThemeProvider theme={dark}> | ||
<CssBaseline /> | ||
<ErrorBoundary> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Praise: I love that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Praise: I love that the |
||
<XServiceProvider> | ||
<AppRouter /> | ||
<GlobalSnackbar /> | ||
</XServiceProvider> | ||
</ErrorBoundary> | ||
</ThemeProvider> | ||
</SWRConfig> | ||
</Router> | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react" | ||
import { RuntimeErrorState } from "../RuntimeErrorState/RuntimeErrorState" | ||
|
||
type ErrorBoundaryProps = Record<string, unknown> | ||
|
||
interface ErrorBoundaryState { | ||
error: Error | null | ||
} | ||
|
||
/** | ||
* Our app's Error Boundary | ||
* Read more about React Error Boundaries: https://reactjs.org/docs/error-boundaries.html | ||
*/ | ||
export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
constructor(props: ErrorBoundaryProps) { | ||
super(props) | ||
this.state = { error: null } | ||
} | ||
|
||
static getDerivedStateFromError(error: Error): { error: Error } { | ||
return { error } | ||
} | ||
|
||
render(): React.ReactNode { | ||
if (this.state.error) { | ||
return <RuntimeErrorState error={this.state.error} /> | ||
} | ||
|
||
return this.props.children | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Button from "@material-ui/core/Button" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import RefreshIcon from "@material-ui/icons/Refresh" | ||
import React from "react" | ||
import { CopyButton } from "../CopyButton/CopyButton" | ||
|
||
const Language = { | ||
reloadApp: "Reload Application", | ||
copyReport: "Copy Report", | ||
} | ||
|
||
/** | ||
* A wrapper component for a full-width copy button | ||
*/ | ||
const CopyStackButton = ({ text }: { text: string }): React.ReactElement => { | ||
const styles = useStyles() | ||
|
||
return ( | ||
<CopyButton | ||
text={text} | ||
ctaCopy={Language.copyReport} | ||
wrapperClassName={styles.buttonWrapper} | ||
buttonClassName={styles.copyButton} | ||
/> | ||
) | ||
} | ||
|
||
/** | ||
* A button that reloads our application | ||
*/ | ||
const ReloadAppButton = (): React.ReactElement => { | ||
const styles = useStyles() | ||
|
||
return ( | ||
<Button | ||
className={styles.buttonWrapper} | ||
variant="outlined" | ||
color="primary" | ||
startIcon={<RefreshIcon />} | ||
onClick={() => location.replace("/")} | ||
> | ||
{Language.reloadApp} | ||
</Button> | ||
) | ||
} | ||
|
||
/** | ||
* createCtas generates an array of buttons to be used with our error boundary UI | ||
*/ | ||
export const createCtas = (codeBlock: string[]): React.ReactElement[] => { | ||
// REMARK: we don't have to worry about key order changing | ||
// eslint-disable-next-line react/jsx-key | ||
return [<CopyStackButton text={codeBlock.join("\r\n")} />, <ReloadAppButton />] | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
buttonWrapper: { | ||
marginTop: theme.spacing(1), | ||
marginLeft: 0, | ||
flex: theme.spacing(1), | ||
textTransform: "uppercase", | ||
}, | ||
|
||
copyButton: { | ||
width: "100%", | ||
marginRight: theme.spacing(1), | ||
backgroundColor: theme.palette.primary.main, | ||
textTransform: "uppercase", | ||
}, | ||
})) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import React from "react" | ||
import { CodeBlock } from "../CodeBlock/CodeBlock" | ||
import { createCtas } from "./ReportButtons" | ||
|
||
const Language = { | ||
reportLoading: "Generating crash report...", | ||
} | ||
|
||
interface ReportState { | ||
error: Error | ||
mappedStack: string[] | null | ||
} | ||
|
||
interface StackTraceAvailableMsg { | ||
type: "stackTraceAvailable" | ||
stackTrace: string[] | ||
} | ||
|
||
/** | ||
* stackTraceUnavailable is a Msg describing a stack trace not being available | ||
*/ | ||
export const stackTraceUnavailable = { | ||
type: "stackTraceUnavailable", | ||
} as const | ||
|
||
type ReportMessage = StackTraceAvailableMsg | typeof stackTraceUnavailable | ||
|
||
export const stackTraceAvailable = (stackTrace: string[]): StackTraceAvailableMsg => { | ||
return { | ||
type: "stackTraceAvailable", | ||
stackTrace, | ||
} | ||
} | ||
|
||
const setStackTrace = (model: ReportState, mappedStack: string[]): ReportState => { | ||
return { | ||
...model, | ||
mappedStack, | ||
} | ||
} | ||
|
||
export const reducer = (model: ReportState, msg: ReportMessage): ReportState => { | ||
switch (msg.type) { | ||
case "stackTraceAvailable": | ||
return setStackTrace(model, msg.stackTrace) | ||
case "stackTraceUnavailable": | ||
return setStackTrace(model, ["Unable to get stack trace"]) | ||
} | ||
} | ||
|
||
/** | ||
* A code block component that contains the error stack resulting from an error boundary trigger | ||
*/ | ||
export const RuntimeErrorReport = ({ error, mappedStack }: ReportState): React.ReactElement => { | ||
const styles = useStyles() | ||
|
||
if (!mappedStack) { | ||
return <CodeBlock lines={[Language.reportLoading]} className={styles.codeBlock} /> | ||
} | ||
|
||
const codeBlock = [ | ||
"======================= STACK TRACE ========================", | ||
"", | ||
error.message, | ||
...mappedStack, | ||
"", | ||
"============================================================", | ||
] | ||
|
||
return <CodeBlock lines={codeBlock} className={styles.codeBlock} ctas={createCtas(codeBlock)} /> | ||
} | ||
|
||
const useStyles = makeStyles(() => ({ | ||
codeBlock: { | ||
minHeight: "auto", | ||
userSelect: "all", | ||
width: "100%", | ||
}, | ||
})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Thanks for pinning this!