mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 16:39:05 +00:00
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.
88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import * as Sentry from '@sentry/nextjs'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { captureCriticalError } from './error-reporting'
|
|
import { classifyResponseError } from '@/data/error-classification'
|
|
|
|
vi.mock('@sentry/nextjs', () => ({
|
|
captureException: vi.fn(),
|
|
withScope: vi.fn((cb: (scope: any) => void) => {
|
|
const scope = { setTag: vi.fn() }
|
|
cb(scope)
|
|
}),
|
|
}))
|
|
|
|
describe('captureCriticalError', () => {
|
|
it.each<[string, Parameters<typeof captureCriticalError>[0]]>([
|
|
['an object with an empty message', { message: '' }],
|
|
['an Error with an empty message', new Error('')],
|
|
['an object without a message', { foo: 'bar' } as unknown as { message: string }],
|
|
['a whitelisted message', new Error('email must be an email')],
|
|
[
|
|
'a message containing a whitelisted message',
|
|
new Error('User error: A user with this email already exists in the system'),
|
|
],
|
|
[
|
|
'a whitelisted password error',
|
|
new Error('Password is known to be weak and easy to guess, please choose a different one'),
|
|
],
|
|
['a whitelisted TOTP error', new Error('Invalid TOTP code entered')],
|
|
['a whitelisted project name error', new Error('name should not contain a . string')],
|
|
[
|
|
'a 4XX StudioError',
|
|
classifyResponseError({ message: 'Not found', code: 404, requestPathname: '/api/test' }),
|
|
],
|
|
[
|
|
'a StudioError at the 4XX boundary (499)',
|
|
classifyResponseError({
|
|
message: 'Client closed request',
|
|
code: 499,
|
|
requestPathname: '/api/test',
|
|
}),
|
|
],
|
|
])('does not capture %s', (_, error) => {
|
|
captureCriticalError(error, 'context')
|
|
|
|
expect(Sentry.captureException).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it.each<[string, Parameters<typeof captureCriticalError>[0], string]>([
|
|
['a regular Error', new Error('Database connection failed'), 'Database connection failed'],
|
|
[
|
|
'a message similar to, but not matching, a whitelisted one',
|
|
new Error('email format is invalid'),
|
|
'email format is invalid',
|
|
],
|
|
['an unknown object with a message', { message: 'Custom error object' }, 'Custom error object'],
|
|
[
|
|
'a StudioError at the 5XX boundary (500)',
|
|
classifyResponseError({
|
|
message: 'Internal server error',
|
|
code: 500,
|
|
requestPathname: '/api/test',
|
|
}),
|
|
'requestPathname /api/test w/ message: Internal server error',
|
|
],
|
|
[
|
|
'a 5XX StudioError',
|
|
classifyResponseError({
|
|
message: 'Gateway timeout',
|
|
code: 504,
|
|
requestPathname: '/api/gateway',
|
|
}),
|
|
'requestPathname /api/gateway w/ message: Gateway timeout',
|
|
],
|
|
[
|
|
'a StudioError without code or requestPathname',
|
|
classifyResponseError({ message: 'Unknown error' }),
|
|
'Response Error (no code or requestPathname) w/ message: Unknown error',
|
|
],
|
|
])('captures %s', (_, error, message) => {
|
|
captureCriticalError(error, 'context')
|
|
|
|
expect(Sentry.captureException).toHaveBeenCalledWith(
|
|
expect.objectContaining({ message, name: 'CriticalError' })
|
|
)
|
|
})
|
|
})
|