Skip to content

Commit

Permalink
feat: add project-level error boundary
Browse files Browse the repository at this point in the history
Closes #47
  • Loading branch information
stdavis committed Jul 15, 2022
1 parent 61b8af8 commit 234c43c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/components/ErrorFallback.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import PropTypes from 'prop-types';
import { Alert, Button } from 'reactstrap';

export default function ErrorFallback({ error, resetErrorBoundary }) {
return (
<Alert color="danger" className="m-3">
<h4 className="alert-heading">Error!</h4>
<p>Looks like something went wrong.</p>
<pre>{error.message}</pre>
<Button color="primary" onClick={resetErrorBoundary}>
Try Again
</Button>
</Alert>
);
}

ErrorFallback.propTypes = {
error: PropTypes.object.isRequired,
resetErrorBoundary: PropTypes.func.isRequired,
};
6 changes: 5 additions & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ErrorBoundary } from 'react-error-boundary';
import ErrorFallback from './components/ErrorFallback';
import './index.css';
import { setConfigs } from './services/config';

Expand All @@ -13,7 +15,9 @@ fetch('config.json')
console.log('rendering app');
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => document.location.reload()}>
<App />
</ErrorBoundary>
</React.StrictMode>
);
});

0 comments on commit 234c43c

Please sign in to comment.