feat: decide whether an error backend is configured when a caught error is logged rather than when lib/observability loads, so tests that mock the env without PostHog settings import it again

This commit is contained in:
Matthew Meszaros
2026-11-08 10:44:49 +01:00
parent 023e5f1cab
commit b7bce86e6d
5 changed files with 15 additions and 11 deletions
+4 -2
View File
@@ -53,8 +53,10 @@ let identity: Identity = null;
let awaiting = 0;
let removeEarlyHandlers: (() => void) | null = null;
// REPORTS_ERRORS is whether a configured backend receives captureException; without one the console is the only record.
export const REPORTS_ERRORS = (Boolean(POSTHOG_KEY) && POSTHOG_ERROR_TRACKING) || Boolean(SENTRY_DSN);
// reportsErrors is whether a configured backend receives captureException; without one the console is the only record.
export function reportsErrors(): boolean {
return (Boolean(POSTHOG_KEY) && POSTHOG_ERROR_TRACKING) || Boolean(SENTRY_DSN);
}
// initErrorReporting is called once, before the app renders. The name is
// historical: on PostHog it is also what starts analytics and session replay,
+3 -3
View File
@@ -16,7 +16,7 @@ import "@fontsource/inter/500.css";
import "@fontsource/inter/600.css";
import { ReauthCancelledError, SessionExpiredError } from "@/lib/api/client";
import { captureException, initErrorReporting, REPORTS_ERRORS } from "@/lib/observability";
import { captureException, initErrorReporting, reportsErrors } from "@/lib/observability";
import { reportFailure } from "@/lib/api/reportFailure";
import { Toaster } from "@/components/ui/sonner";
@@ -223,12 +223,12 @@ initErrorReporting();
// A caught error is reported by reportRouteError below; React's console copy would report it twice.
const logCaught = (error: unknown, info: { componentStack?: string }) => {
if (import.meta.env.DEV || !REPORTS_ERRORS) console.error(error, info.componentStack);
if (import.meta.env.DEV || !reportsErrors()) console.error(error, info.componentStack);
};
// The one report for every route's render, loader and action errors, including routes with no errorElement.
const reportRouteError = (error: unknown) => {
if (import.meta.env.DEV || !REPORTS_ERRORS) console.error(error);
if (import.meta.env.DEV || !reportsErrors()) console.error(error);
// A lapsed session is the sign-in screen's job, and a closed confirm prompt is a choice.
if (error instanceof SessionExpiredError || error instanceof ReauthCancelledError) return;
captureException(error);
+2 -2
View File
@@ -12,7 +12,7 @@
import React from "react";
import { useLocation, useRouter } from "@tanstack/react-router";
import { AlertTriangleIcon, RefreshCcwIcon } from "lucide-react";
import { captureException, REPORTS_ERRORS } from "@/lib/observability";
import { captureException, reportsErrors } from "@/lib/observability";
import { SENTRY_RELEASE } from "@/lib/information";
import { canReloadForStaleChunk, isModuleLoadError, reloadForStaleChunk } from "@/lib/moduleLoadError";
@@ -52,7 +52,7 @@ export class ErrorBoundary extends React.Component<BoundaryProps, State> {
// SDK only when it had put itself there, which a bundled one does not.
captureException(error);
// Reported just above; console capture would file the same error again.
if (import.meta.env.DEV || !REPORTS_ERRORS) console.error("[ErrorBoundary]", error, info?.componentStack);
if (import.meta.env.DEV || !reportsErrors()) console.error("[ErrorBoundary]", error, info?.componentStack);
}
reset = () => {
+4 -2
View File
@@ -53,8 +53,10 @@ let identity: Identity = null;
let awaiting = 0;
let removeEarlyHandlers: (() => void) | null = null;
// REPORTS_ERRORS is whether a configured backend receives captureException; without one the console is the only record.
export const REPORTS_ERRORS = (Boolean(POSTHOG_KEY) && POSTHOG_ERROR_TRACKING) || Boolean(SENTRY_DSN);
// reportsErrors is whether a configured backend receives captureException; without one the console is the only record.
export function reportsErrors(): boolean {
return (Boolean(POSTHOG_KEY) && POSTHOG_ERROR_TRACKING) || Boolean(SENTRY_DSN);
}
// initErrorReporting is called once, before the app renders.
export function initErrorReporting(): void {
+2 -2
View File
@@ -8,7 +8,7 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools"
import { RouterProvider } from "@tanstack/react-router";
import { Toaster } from '@/components/ui/toaster';
import { initErrorReporting, REPORTS_ERRORS } from "@/lib/observability";
import { initErrorReporting, reportsErrors } from "@/lib/observability";
import { initProductAnalytics } from "@/lib/productAnalytics";
import { installDomMutationGuard } from "@/lib/domGuard";
import { queryClient } from "@/lib/queryClient";
@@ -24,7 +24,7 @@ initProductAnalytics();
// A caught error is reported by the boundary that caught it; React's console copy would report it twice.
const logCaught = (error: unknown, info: { componentStack?: string }) => {
if (import.meta.env.DEV || !REPORTS_ERRORS) console.error(error, info.componentStack)
if (import.meta.env.DEV || !reportsErrors()) console.error(error, info.componentStack)
}
const rootEl = document.getElementById('root')!