Files
supabase/apps/studio/lib/errors/error-routing.ts
T
supabase-vercel-tedd[bot] f0986f5f24 fix(studio): treat 401 API errors as a user fault for Sentry routing (#53411)
## Problem

A 401 from the platform API means the browser sent no valid session
token: the user signed out, or the session expired. Studio then
redirects to sign-in. `classifyResponseError` turns any 4xx into
`UnclassifiedApiError` with `fault: 'studio'`, so every query that fails
while the session is gone goes to Sentry at the Studio-owned sample
rate. The first query to fail is often the read-replicas query. Sentry
fills with errors that Studio can't fix.

## Solution

- `UnclassifiedApiError` now derives its fault from the status: a 401 is
`user`, and every other 4xx stays `studio`.
- `ERROR_ROUTING.UnclassifiedApiError` gets a `user` entry at
`SAMPLE_USER_FAULT`. 401s still send a small trickle to catch
misclassification, and 403/404/429 keep `SAMPLE_OWNED`.
- Because the fault is `user`, `AlertError` shows the user-fault
presentation for a 401 ("try again / refresh"), with no contact-support
prompt.
- Tests: `handleError.test.ts` expects `user` for a 401, and
`error-routing.test.ts` covers the 401 and 403 routing.

## Review instructions

This change has no new UI. To check it:

1. In `apps/studio`, run `vitest run lib/errors data/handleError.test.ts
data/error-classification.test.ts data/fetchers-errors.test.ts
data/query-client-reporting.test.ts components/ui`. All tests pass.
2. In `apps/studio`, run `eslint` on the changed files. It reports
nothing.
3. `tsc --noEmit` in `apps/studio` reports one error, in
`packages/ui-patterns/src/McpUrlBuilder/components/InstructionBlocks.tsx`.
The same error appears without this change: it comes from running `tsc`
without `next typegen` first.


[FE-5086](https://linear.app/supabase/issue/FE-5086/unclassifiedapierror-no-access-token-provided)

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-11-18 16:48:25 +07:00

75 lines
3.2 KiB
TypeScript

import type { Fault, StudioError, 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
)
/**
* Cap for errors whose fault is the user's. A trickle to catch misclassification and Studio bugs
* hiding in user faults.
*/
export const SAMPLE_USER_FAULT = sampleRateFromEnv(
process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE_USER_FAULT,
0.01
)
/** Always send (also used when a caller forces capture). */
export const SAMPLE_ALWAYS = 1
export type Routing = {
sentrySampleRate: number
// TODO: add PostHog routing when errors are sent to PostHog
}
// For every tag, requires exactly the faults that class can have, so a new tag or a new possible
// fault fails typecheck until it is routed.
type ErrorRoutingTable = {
[Tag in StudioErrorTag]: Record<Extract<StudioError, { _tag: Tag }>['fault'], Routing>
}
// The class decides privacy (`isNeverReportedError`); this table gives the rate for each tag and
// fault. A user fault is rarely ours to fix, so it only gets a trickle (`SAMPLE_USER_FAULT`) to catch
// misclassification and Studio bugs hiding in it.
export const ERROR_ROUTING = {
RequestAborted: { user: { sentrySampleRate: SAMPLE_NEVER } },
NetworkUnreachable: {
user: { sentrySampleRate: SAMPLE_USER_FAULT }, // reason `offline`
platform: { sentrySampleRate: SAMPLE_OTHER },
},
MalformedResponse: { platform: { sentrySampleRate: SAMPLE_ALWAYS } },
PlatformError: { platform: { sentrySampleRate: SAMPLE_OTHER } },
UnclassifiedApiError: {
user: { sentrySampleRate: SAMPLE_USER_FAULT }, // 401, no valid session
studio: { sentrySampleRate: SAMPLE_OWNED },
},
DbConnectionTimeout: { platform: { sentrySampleRate: SAMPLE_OTHER } },
UserSqlError: { user: { sentrySampleRate: SAMPLE_NEVER } },
StudioSqlError: {
user: { sentrySampleRate: SAMPLE_USER_FAULT }, // reason `user_input`
studio: { sentrySampleRate: SAMPLE_OWNED },
},
UnexpectedError: { studio: { sentrySampleRate: SAMPLE_OWNED } },
} as const satisfies ErrorRoutingTable
/** The routing for one error, by its tag and fault. */
export function getErrorRouting(error: StudioError): Routing {
// TypeScript can't correlate `_tag` with `fault` across the union, so view the table by plain
// keys. `ErrorRoutingTable` guarantees every real tag and fault pair is present.
const byFault: Partial<Record<Fault, Routing>> = ERROR_ROUTING[error._tag]
return byFault[error.fault] ?? { sentrySampleRate: SAMPLE_NEVER }
}