Skip to content

Commit

Permalink
feat: error reporting callback (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Sep 9, 2022
1 parent d42d440 commit 0c74dae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ interface Props {
lockedApiSettings?: boolean
isDesktop?: boolean
desktopUrl?: string
errorReporting?: (err: Error) => void
}

const App = ({ beeApiUrl, beeDebugApiUrl, lockedApiSettings, isDesktop, desktopUrl }: Props): ReactElement => {
const App = ({
beeApiUrl,
beeDebugApiUrl,
lockedApiSettings,
isDesktop,
desktopUrl,
errorReporting,
}: Props): ReactElement => {
const mainApp = (
<div className="App">
<ThemeProvider theme={theme}>
Expand All @@ -46,7 +54,7 @@ const App = ({ beeApiUrl, beeDebugApiUrl, lockedApiSettings, isDesktop, desktopU
<Router>
<>
<CssBaseline />
<Dashboard>
<Dashboard errorReporting={errorReporting}>
<BaseRouter />
</Dashboard>
</>
Expand Down
9 changes: 8 additions & 1 deletion src/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { Component, ErrorInfo, ReactElement } from 'react'

interface Props {
children: ReactElement
errorReporting?: (err: Error) => void
}

interface State {
error: Error | null
}

export default class ErrorBoundary extends Component<Props, State> {
private errorReporting?: (err: Error) => void

constructor(props: Props) {
super(props)
this.errorReporting = props.errorReporting
this.state = { error: null }
}

Expand All @@ -20,7 +24,10 @@ export default class ErrorBoundary extends Component<Props, State> {
}

componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
// You can also log the error to an error reporting service
if (this.errorReporting) {
this.errorReporting(error)
}

console.error({ error, errorInfo }) // eslint-disable-line
}

Expand Down
3 changes: 2 additions & 1 deletion src/layout/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const useStyles = makeStyles((theme: Theme) =>

interface Props {
children?: ReactElement
errorReporting?: (err: Error) => void
}

const Dashboard = (props: Props): ReactElement => {
Expand Down Expand Up @@ -119,7 +120,7 @@ const Dashboard = (props: Props): ReactElement => {
<SideBar />
<Container className={classes.content}>
{' '}
<ErrorBoundary>{content}</ErrorBoundary>
<ErrorBoundary errorReporting={props.errorReporting}>{content}</ErrorBoundary>
</Container>
</div>
)
Expand Down

0 comments on commit 0c74dae

Please sign in to comment.