import * as Sentry from '@sentry/nextjs' import type { NextPageContext } from 'next' import NextError, { type ErrorProps } from 'next/error' import { useEffect, useRef } from 'react' // Relative import: the Sentry browser vitest project has no tsconfig path aliases import { reportGlobalCrashMetric } from '../lib/frontend-metrics' export default function CustomError(props: ErrorProps) { const { statusCode } = props const errorRef = useRef(null) useEffect(() => { errorRef.current?.focus() }, []) useEffect(() => { // Skip 404s and other client errors; only crashes count. Next leaves the // status code unset when a client-side error has none. // This page gets no error object, so the effect dependency is what keeps // re-renders from counting the same crash again. if (!statusCode || statusCode >= 500) reportGlobalCrashMetric(undefined) }, [statusCode]) return (
) } CustomError.getInitialProps = async (context: NextPageContext) => { await Sentry.withScope(async (scope) => { scope.setTag('globalErrorBoundary', true) await Sentry.captureUnderscoreErrorException(context) }) return NextError.getInitialProps(context) }