Skip to content

Commit

Permalink
feat(web): wrap app in errorboundary
Browse files Browse the repository at this point in the history
Prevents the app from failing on render errors as mentioned
in #675. More to come..
  • Loading branch information
thelindat committed Dec 6, 2024
1 parent 4d7fee4 commit aebef6d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
16 changes: 10 additions & 6 deletions web/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
import { fas } from '@fortawesome/free-solid-svg-icons';
Expand All @@ -9,6 +9,7 @@ import { library } from '@fortawesome/fontawesome-svg-core';
import { isEnvBrowser } from './utils/misc';
import LocaleProvider from './providers/LocaleProvider';
import ConfigProvider from './providers/ConfigProvider';
import ErrorBoundary from './providers/errorBoundary';

library.add(fas, far, fab);

Expand All @@ -23,12 +24,15 @@ if (isEnvBrowser()) {
}

const root = document.getElementById('root');
ReactDOM.createRoot(root!).render(
<React.StrictMode>

createRoot(root!).render(
<StrictMode>
<LocaleProvider>
<ConfigProvider>
<App />
<ErrorBoundary>
<App />
</ErrorBoundary>
</ConfigProvider>
</LocaleProvider>
</React.StrictMode>
</StrictMode>
);
23 changes: 23 additions & 0 deletions web/src/providers/errorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Component, ReactNode, ErrorInfo } from 'react';

class ErrorBoundary extends Component<{ children: ReactNode }, { hasError: boolean }> {
constructor(props: { children: ReactNode }) {
super(props);
this.state = { hasError: false };
}

static getDerivedStateFromError(err: Error) {
return { hasError: true };
}

componentDidCatch(error: Error, info: ErrorInfo) {
console.error(error, info)
this.setState({ hasError: false });
}

render() {
return this.state.hasError ? null : this.props.children;
}
}

export default ErrorBoundary;

0 comments on commit aebef6d

Please sign in to comment.