Full Toolkit for Next-Level Domain Analysis
Add page and browser deployment context to boundary errors ππ§ (#143)
Felix Wotschofskyd3d1cbbc2bd9
diff --git a/components/boundary-error.test.tsx b/components/boundary-error.test.tsxnew file mode 100644index 0000000..2bb5832--- /dev/null+++ b/components/boundary-error.test.tsx@@ -0,0 +1,68 @@+import { act } from 'react';+import { createRoot } from 'react-dom/client';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { BoundaryError } from './boundary-error';++const { report } = vi.hoisted(() => ({ report: vi.fn() }));+vi.mock('evlog/next/client', () => ({ log: { error: report } }));++describe('browser boundary error correlation', () => {+ beforeEach(() => {+ vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);+ vi.stubEnv('NEXT_PUBLIC_DEPLOYMENT_COMMIT', 'browser-commit');+ vi.stubEnv('NEXT_PUBLIC_DEPLOYMENT_ID', 'browser-deployment');+ window.history.replaceState({}, '', '/lookup/example.com/dnssec?q=private');+ });++ afterEach(() => {+ window.history.replaceState({}, '', '/');+ vi.unstubAllEnvs();+ vi.unstubAllGlobals();+ vi.clearAllMocks();+ });++ it.each([+ [+ 'Minified React error #412; visit the decoder',+ 'connection_closed',+ '412',+ ],+ ['Minified React error #441; visit the decoder', 'server_render', '441'],+ ['Unexpected application failure', 'application', undefined],+ ])(+ 'reports the route without its query string and the browser build for %s',+ async (message, category, reactErrorCode) => {+ const container = document.createElement('div');+ const root = createRoot(container);++ const error = Object.assign(new Error(message), {+ digest: 'server-digest',+ });++ try {+ await act(async () =>+ root.render(<BoundaryError error={error} retry={() => {}} />),+ );++ expect(report).toHaveBeenCalledWith(+ expect.objectContaining({+ message: 'boundary_error',+ page: '/lookup/example.com/dnssec',+ browserCommit: 'browser-commit',+ browserDeploymentId: 'browser-deployment',+ digest: 'server-digest',+ category,+ reactErrorCode,+ }),+ );++ expect(JSON.stringify(report.mock.calls[0][0])).not.toContain(+ 'q=private',+ );+ } finally {+ await act(async () => root.unmount());+ }+ },+ );+});diff --git a/components/boundary-error.tsx b/components/boundary-error.tsxindex 8142e6b..0b1ab1b 100644--- a/components/boundary-error.tsx+++ b/components/boundary-error.tsx@@ -6,6 +6,7 @@ import { type FC, useEffect, useState } from 'react';import { Button } from '@/components/ui/button';import { Spinner } from '@/components/ui/spinner';+import { getBoundaryErrorContext } from '@/lib/boundary-error-context';import { parseUserFacingDigest } from '@/lib/user-facing-error';type BoundaryErrorProps = {@@ -22,13 +23,7 @@ export const BoundaryError: FC<BoundaryErrorProps> = ({const [isPending, setIsPending] = useState(false);useEffect(() => {- log.error({- message: 'boundary_error',- name: error.name,- error: error.message,- digest: error.digest,- stack: error.stack,- });+ log.error(getBoundaryErrorContext(error));}, [error]);const userFacing = parseUserFacingDigest(error.digest);diff --git a/lib/boundary-error-context.ts b/lib/boundary-error-context.tsnew file mode 100644index 0000000..299696f--- /dev/null+++ b/lib/boundary-error-context.ts@@ -0,0 +1,23 @@+const REACT_ERROR_CATEGORIES: Partial<Record<string, string>> = {+ '412': 'connection_closed',+ '441': 'server_render',+};++export const getBoundaryErrorContext = (error: Error & { digest?: string }) => {+ const reactErrorCode = /Minified React error #(\d+)/.exec(error.message)?.[1];++ return {+ message: 'boundary_error',+ name: error.name,+ error: error.message,+ digest: error.digest,+ stack: error.stack,+ page: typeof window === 'undefined' ? undefined : window.location.pathname,+ browserCommit: process.env.NEXT_PUBLIC_DEPLOYMENT_COMMIT,+ browserDeploymentId: process.env.NEXT_PUBLIC_DEPLOYMENT_ID,+ reactErrorCode,+ category:+ (reactErrorCode && REACT_ERROR_CATEGORIES[reactErrorCode]) ??+ 'application',+ };+};diff --git a/next.config.ts b/next.config.tsindex 658d8c0..f4b6cc9 100644--- a/next.config.ts+++ b/next.config.ts@@ -4,6 +4,11 @@ import { getSponsorImageRemotePatterns } from './lib/sponsors';const nextConfig = async (): Promise<NextConfig> => ({cacheComponents: true,+ env: {+ // A stale browser bundle reports its own deployment, not the one ingesting the log.+ NEXT_PUBLIC_DEPLOYMENT_COMMIT: process.env.VERCEL_GIT_COMMIT_SHA ?? 'local',+ NEXT_PUBLIC_DEPLOYMENT_ID: process.env.VERCEL_DEPLOYMENT_ID ?? 'local',+ },partialPrefetching: false,reactStrictMode: false,turbopack: {