Improve exception handling

This commit is contained in:
Gilbert 2024-05-02 16:36:31 -05:00
parent aaadfeab72
commit 00199424f7
3 changed files with 31 additions and 21 deletions

View File

@ -13,19 +13,7 @@ import reportWebVitals from './reportWebVitals';
import { GQLClientProvider } from './context/GQLClientContext';
import { SERVER_GQL_PATH } from './constants';
import { Toaster } from 'components/shared/Toast';
import Bugsnag from '@bugsnag/js';
import BugsnagPluginReact from '@bugsnag/plugin-react';
import BugsnagPerformance from '@bugsnag/browser-performance';
const bugsnagApiKey = import.meta.env.VITE_BUGSNAG_API_KEY;
if (bugsnagApiKey) {
Bugsnag.start({
apiKey: bugsnagApiKey,
plugins: [new BugsnagPluginReact()],
});
BugsnagPerformance.start({ apiKey: bugsnagApiKey });
}
import { LogErrorBoundary } from 'utils/log-error';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement,
@ -39,12 +27,8 @@ const gqlEndpoint = `${import.meta.env.VITE_SERVER_URL}/${SERVER_GQL_PATH}`;
const gqlClient = new GQLClient({ gqlEndpoint });
const ErrorBoundary = bugsnagApiKey
? Bugsnag.getPlugin('react')!.createErrorBoundary(React)
: ({ children }: any) => children;
root.render(
<ErrorBoundary>
<LogErrorBoundary>
<React.StrictMode>
<ThemeProvider>
<GQLClientProvider client={gqlClient}>
@ -54,7 +38,7 @@ root.render(
</ThemeProvider>
</React.StrictMode>
,
</ErrorBoundary>,
</LogErrorBoundary>,
);
// If you want to start measuring performance in your app, pass a function

View File

@ -58,9 +58,8 @@ export const Login = ({ onDone }: Props) => {
}
} catch (err: any) {
setError(err.message);
console.log(err.message, err.name, err.details);
setProvider(false);
logError(err);
setProvider(false);
return;
}
}

View File

@ -1,6 +1,33 @@
import Bugsnag from '@bugsnag/js';
import BugsnagPluginReact from '@bugsnag/plugin-react';
import BugsnagPerformance from '@bugsnag/browser-performance';
import React from 'react';
const bugsnagApiKey = import.meta.env.VITE_BUGSNAG_API_KEY;
if (bugsnagApiKey) {
Bugsnag.start({
apiKey: bugsnagApiKey,
plugins: [new BugsnagPluginReact()],
});
BugsnagPerformance.start({ apiKey: bugsnagApiKey });
}
export const errorLoggingEnabled = !!bugsnagApiKey;
export const LogErrorBoundary = bugsnagApiKey
? Bugsnag.getPlugin('react')!.createErrorBoundary(React)
: ({ children }: any) => children;
export function logError(error: Error) {
let errors: any[] = [error];
let safety = 0;
while (errors[errors.length - 1].cause && safety < 10) {
errors.push('::caused by::', errors[errors.length - 1].cause);
safety += 1;
}
console.error(...errors);
if (import.meta.env.VITE_BUGSNAG_API_KEY) {
Bugsnag.notify(error);
}