Files
supabase/apps/studio/lib/errors/error-routing.ts
T
Charis b3dd6612d7 feat(studio): classify errors with fault, reason, and per-tag Sentry sampling (#52730)
Follows #52707 (rename `ResponseError` to `StudioError`).

Resolves FE-4772.

## What

Introduces the error core for Studio error classification.

- **Tagged errors.** `lib/errors/studio-errors.ts` defines
`Data.TaggedError` classes (`RequestAborted`, `NetworkUnreachable`,
`MalformedResponse`, `PlatformError`, `UnclassifiedApiError`,
`DbConnectionTimeout`, `UnexpectedError`). Each carries `fault`,
`reason`, `classifiedBy` (`rule` or `default`), `retryable`, and
`service`. `StudioError` is the union type; use `isStudioError` instead
of `instanceof`.
- **Two classifiers** in `data/error-classification.ts`:
- `classifyResponseError` is only for errors from an API response: known
message patterns first, then defaults by status (5xx `PlatformError`,
4xx `UnclassifiedApiError`, no status `UnexpectedError`).
- `classifyError` is for everything else: aborts, browser fetch failures
(matched by message, so ordinary `TypeError` bugs stay
`UnexpectedError`), and unexpected errors.
- `handleError` picks between them, reports, and rethrows the same
instance so React Query receives the classified error.
- **Routing and sampling.** `lib/errors/error-routing.ts` maps every tag
to a Sentry sample rate (`SAMPLE_NEVER`, `SAMPLE_OTHER`, `SAMPLE_OWNED`,
`SAMPLE_ALWAYS`). `reportStudioError` tags events with `sample_rate`,
and `beforeSend` applies it. `alwaysCapture` reports at rate 1. The
global Sentry error `sampleRate` stays unset so the rates don't
multiply.
- **Network failures.** A fetch failure is a `user` fault with reason
`offline` when the browser reports being offline, and a `platform` fault
otherwise.
- **Hand-built errors.** Call sites that wrapped caught exceptions or
made up status codes now use `classifyError` or a specific class instead
of `classifyResponseError`.

## Behavior changes

- Errors that previously went unreported, including unexpected errors
with a message, are now reported to Sentry at their routed sample rate.
- `handleError` no longer logs the stack with `console.error`.
- `MalformedResponse` is defined but not produced yet; a follow-up adds
the empty-body check.

## Testing

- Unit tests for classification, routing, reporting, and the
`beforeSend` sampling helper.
- `pnpm vitest run data lib components/interfaces/ErrorHandling`,
typecheck, lint ratchet, and knip pass locally.
2026-11-04 13:19:01 -05:00

40 lines
1.7 KiB
TypeScript

import type { StudioErrorTag } from '@/lib/errors/studio-errors'
/** Never send to Sentry. */
export const SAMPLE_NEVER = 0
/** Reads a 0-1 rate from an env var, falling back when it is unset or out of range. */
export function sampleRateFromEnv(value: string | undefined, fallback: number) {
if (value === undefined || value.trim() === '') return fallback
const rate = Number(value)
return Number.isFinite(rate) && rate >= 0 && rate <= 1 ? rate : fallback
}
// Env vars are read with literal `process.env.NEXT_PUBLIC_*` access so the Next and Vite builds can
// inline them.
/** Faults owned by other teams or services: keep a small sample for visibility. */
export const SAMPLE_OTHER = sampleRateFromEnv(process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE_OTHER, 0.1)
/** Faults Studio owns and fixes. */
export const SAMPLE_OWNED = sampleRateFromEnv(
process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE_OWNED,
0.25
)
/** Always send (also used when a caller forces capture). */
export const SAMPLE_ALWAYS = 1
type Routing = {
sentrySampleRate: number
// TODO: add PostHog routing when errors are sent to PostHog
}
// `satisfies Record<StudioErrorTag, ...>` makes a new tag fail typecheck until it is routed here.
export const ERROR_ROUTING = {
RequestAborted: { sentrySampleRate: SAMPLE_NEVER },
NetworkUnreachable: { sentrySampleRate: SAMPLE_OTHER },
MalformedResponse: { sentrySampleRate: SAMPLE_OTHER },
PlatformError: { sentrySampleRate: SAMPLE_OTHER },
UnclassifiedApiError: { sentrySampleRate: SAMPLE_OWNED },
DbConnectionTimeout: { sentrySampleRate: SAMPLE_OTHER },
UnexpectedError: { sentrySampleRate: SAMPLE_OWNED },
} as const satisfies Record<StudioErrorTag, Routing>