Files
supabase/apps/studio/data/error-classification.test.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

118 lines
3.8 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
classifyError,
classifyResponseError,
parseResponseErrorBody,
} from './error-classification'
import {
getServiceForEndpoint,
NetworkUnreachableError,
PlatformError,
RequestAbortedError,
UnexpectedError,
} from '@/lib/errors/studio-errors'
describe('classifyError', () => {
it('returns an already-tagged error unchanged', () => {
const err = new PlatformError({ message: 'x', code: 500 })
expect(classifyError(err)).toBe(err)
})
it.each([
['a DOMException', new DOMException('aborted', 'AbortError')],
['an Error named AbortError', Object.assign(new Error('aborted'), { name: 'AbortError' })],
])('maps %s to RequestAborted and keeps the cause', (_, thrown) => {
const err = classifyError(thrown)
expect(err).toBeInstanceOf(RequestAbortedError)
expect(err.cause).toBe(thrown)
})
it.each(['Failed to fetch', 'NetworkError when attempting to fetch resource.', 'Load failed'])(
'maps a fetch-failure TypeError (%s) to NetworkUnreachable',
(message) => {
expect(classifyError(new TypeError(message))).toBeInstanceOf(NetworkUnreachableError)
}
)
it('attributes a fetch failure to the platform while online', () => {
const err = classifyError(new TypeError('Failed to fetch'))
expect(err.reason).toBe('network_unreachable')
expect(err.fault).toBe('platform')
})
it("attributes a fetch failure to the user's network while offline", () => {
vi.stubGlobal('navigator', { onLine: false })
const err = classifyError(new TypeError('Failed to fetch'))
vi.unstubAllGlobals()
expect(err.reason).toBe('offline')
expect(err.fault).toBe('user')
})
it('maps a programming-bug TypeError to UnexpectedError', () => {
const err = classifyError(new TypeError("Cannot read properties of undefined (reading 'x')"))
expect(err).toBeInstanceOf(UnexpectedError)
expect(err.fault).toBe('studio')
})
it('maps anything else to UnexpectedError with the cause', () => {
const thrown = new RangeError('nope')
const err = classifyError(thrown, { endpoint: '/v1/projects/{ref}', method: 'GET' })
expect(err).toBeInstanceOf(UnexpectedError)
expect(err.cause).toBe(thrown)
expect(err.service).toBe('management-api')
expect(err.method).toBe('GET')
})
})
describe('classifyResponseError', () => {
it('passes endpoint and method through', () => {
const err = classifyResponseError({
message: 'x',
code: 500,
endpoint: '/platform/projects/{ref}',
method: 'GET',
})
expect(err.endpoint).toBe('/platform/projects/{ref}')
expect(err.service).toBe('management-api')
})
})
describe('parseResponseErrorBody', () => {
it('reads the fields the fetch middleware adds', () => {
const body = { msg: 'boom', code: 503, requestId: 'r1', retryAfter: 5, requestPathname: '/x' }
expect(parseResponseErrorBody(body)).toMatchObject({
message: 'boom',
code: 503,
requestId: 'r1',
retryAfter: 5,
requestPathname: '/x',
})
})
it('keeps the status of a body that has a code but no message', () => {
expect(parseResponseErrorBody({ code: 500, retryAfter: 5 })).toMatchObject({
message: undefined,
code: 500,
retryAfter: 5,
})
})
it('returns undefined without a message or a code', () => {
expect(parseResponseErrorBody({ requestId: 'r1' })).toBeUndefined()
expect(parseResponseErrorBody(undefined)).toBeUndefined()
})
})
describe('getServiceForEndpoint', () => {
it.each([
['/platform/pg-meta/{ref}/query', 'pg-meta'],
['/v1/projects/{ref}/functions', 'management-api'],
['/platform/projects/{ref}', 'management-api'],
['/api/ai/sql', 'unknown'],
[undefined, 'unknown'],
])('maps %s to %s', (endpoint, service) => {
expect(getServiceForEndpoint(endpoint)).toBe(service)
})
})