shellphone.app/app/pages/_app.tsx
m5r bdac93d7e1 * custom error component
* upload sourcemaps to sentry
* report caught errors to sentry
2021-09-07 04:49:13 +08:00

58 lines
1.7 KiB
TypeScript

import { Suspense, useEffect } from "react";
import {
AppProps,
ErrorBoundary,
AuthenticationError,
AuthorizationError,
ErrorFallbackProps,
useQueryErrorResetBoundary,
getConfig,
useSession,
} from "blitz";
import Sentry from "../../integrations/sentry";
import ErrorComponent from "../core/components/error-component";
import LoginForm from "../auth/components/login-form";
import { usePanelbear } from "../core/hooks/use-panelbear";
import "app/core/styles/index.css";
const { publicRuntimeConfig } = getConfig();
export default function App({ Component, pageProps }: AppProps) {
const session = useSession();
usePanelbear(publicRuntimeConfig.panelBear.siteId);
useEffect(() => {
if (session.userId) {
Sentry.setUser({
id: session.userId.toString(),
orgId: session.orgId,
});
}
}, [session]);
const getLayout = Component.getLayout || ((page) => page);
return (
<ErrorBoundary
onError={(error, componentStack) =>
Sentry.captureException(error, { contexts: { react: { componentStack } } })
}
FallbackComponent={RootErrorFallback}
onReset={useQueryErrorResetBoundary().reset}
>
<Suspense fallback="Silence, ca pousse">{getLayout(<Component {...pageProps} />)}</Suspense>
</ErrorBoundary>
);
}
function RootErrorFallback({ error, resetErrorBoundary }: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <LoginForm onSuccess={resetErrorBoundary} />;
} else if (error instanceof AuthorizationError) {
return <ErrorComponent statusCode={error.statusCode} title="Sorry, you are not authorized to access this" />;
} else {
return <ErrorComponent statusCode={error.statusCode || 400} title={error.message || error.name} />;
}
}