All files / src/lib error-reporting.ts

100% Statements 21/21
83.33% Branches 10/12
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          8x             4x   3x                                                         4x 3x 3x   1x 1x           3x 3x 3x 1x   1x 2x 1x 1x 1x 1x   3x    
import * as Sentry from '@sentry/react'
import { ZodError } from 'zod'
 
import { ApiError } from './http-client'
 
const dsn = import.meta.env.VITE_SENTRY_DSN
 
/**
 * No-ops until VITE_SENTRY_DSN is set (local builds have no DSN).
 * Call once, before the app renders.
 */
export function initErrorReporting(): void {
  if (!dsn) return
 
  Sentry.init({
    dsn,
    integrations: [
      // Not a default integration: getDefaultIntegrations() ships InboundFilters,
      // GlobalHandlers, Breadcrumbs and friends, but no tracing. Without this the
      // SDK reports errors and silently produces no spans — so tracesSampleRate
      // alone measured nothing. This is also what records the Web Vitals
      // (LCP, CLS, INP, TTFB, FCP) as measurements on the pageload span.
      //
      // It costs 19 kB gzip on the entry chunk (119 → 138 kB, measured), which
      // lands on the critical path and therefore worsens the very LCP it
      // reports. Accepted: an unmeasured LCP cannot be improved, and the
      // alternative that is cheap (the web-vitals package, ~2 kB) reports the
      // numbers without the pageload span that says which request caused them.
      Sentry.browserTracingIntegration(),
    ],
    // The site gets portfolio-scale traffic. At 0.1 a whole day can pass with no
    // sampled pageload, which reads as "no data" rather than "no traffic" and
    // makes the numbers untrustworthy. Revisit if volume ever justifies it.
    tracesSampleRate: 1.0,
    // Same-origin only. The API is reached through a proxy at /api/*, so it is
    // same-origin too. This matters because photoUrl and avatarUrl accept any
    // host by design — without a list, trace headers would ride along to
    // whatever third party a user pasted in.
    tracePropagationTargets: [/^\//],
  })
}
 
export function reportError(error: unknown, context?: Record<string, unknown>): void {
  if (dsn) {
    Sentry.captureException(error, { extra: context })
    return
  }
  Eif (import.meta.env.DEV) {
    console.error('[error-reporting]', error, context)
  }
}
 
/** Report technical failures without API bodies, submitted values or child IDs. */
export function reportRequestError(error: unknown, operation: 'query' | 'mutation'): void {
  const context: Record<string, unknown> = { operation }
  let message = 'Unexpected request failure'
  if (error instanceof ZodError) {
    message = 'API schema validation failed'
    // Zod messages and enum "received" values can include submitted personal data.
    context.issues = error.issues.map(({ code, path }) => ({ code, path }))
  } else if (error instanceof ApiError) {
    message = `API request failed (HTTP ${error.status})`
    context.status = error.status
  } else Eif (error instanceof TypeError) {
    message = 'Request failed with TypeError'
  }
  reportError(new Error(message), context)
}