Files
supabase/apps/studio/lib/errors/error-routing.test.ts
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

63 lines
1.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
getErrorRouting,
SAMPLE_NEVER,
SAMPLE_OTHER,
SAMPLE_OWNED,
SAMPLE_USER_FAULT,
sampleRateFromEnv,
} from './error-routing'
import {
NetworkUnreachableError,
PlatformError,
RequestAbortedError,
StudioSqlError,
UnclassifiedApiError,
UnexpectedError,
UserSqlError,
} from './studio-errors'
describe('sampleRateFromEnv', () => {
it.each([
['0.5', 0.5],
['0', 0],
['1', 1],
])('uses a valid rate (%s)', (value, rate) => {
expect(sampleRateFromEnv(value, 0.1)).toBe(rate)
})
it.each([undefined, '', ' ', 'abc', '-0.1', '1.5'])('falls back for %j', (value) => {
expect(sampleRateFromEnv(value, 0.1)).toBe(0.1)
})
})
describe('getErrorRouting', () => {
it.each([
['StudioSqlError user_input', new StudioSqlError({ reason: 'user_input' }), SAMPLE_USER_FAULT],
['StudioSqlError studio_sql', new StudioSqlError({}), SAMPLE_OWNED],
['UserSqlError', new UserSqlError({}), SAMPLE_NEVER],
['RequestAborted', new RequestAbortedError({}), SAMPLE_NEVER],
[
'NetworkUnreachable offline',
new NetworkUnreachableError({ reason: 'offline' }),
SAMPLE_USER_FAULT,
],
['NetworkUnreachable', new NetworkUnreachableError({}), SAMPLE_OTHER],
['PlatformError', new PlatformError({}), SAMPLE_OTHER],
[
'UnclassifiedApiError 401',
new UnclassifiedApiError({ code: 401, reason: 'unauthorized' }),
SAMPLE_USER_FAULT,
],
[
'UnclassifiedApiError 403',
new UnclassifiedApiError({ code: 403, reason: 'forbidden' }),
SAMPLE_OWNED,
],
['UnexpectedError', new UnexpectedError({}), SAMPLE_OWNED],
])('routes %s', (_, error, rate) => {
expect(getErrorRouting(error).sentrySampleRate).toBe(rate)
})
})