mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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.
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { queryOptions } from '@tanstack/react-query'
|
|
|
|
import { platformKeys } from './keys'
|
|
import { classifyResponseError } from '@/data/error-classification'
|
|
import { BASE_PATH, IS_PLATFORM } from '@/lib/constants'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { isStudioError } from '@/lib/errors/studio-errors'
|
|
import {
|
|
StatusPageResponseSchema,
|
|
type StatusPageResponse,
|
|
} from '@/lib/status-page/status-page.schema'
|
|
|
|
async function getStatusPage(signal?: AbortSignal): Promise<StatusPageResponse> {
|
|
const response = await fetch(`${BASE_PATH}/api/status-page`, {
|
|
signal,
|
|
method: 'GET',
|
|
credentials: 'omit',
|
|
})
|
|
|
|
if (!response.ok) {
|
|
let retryAfter: number | undefined
|
|
const retryAfterHeader = response.headers.get('Retry-After')
|
|
if (retryAfterHeader !== null) {
|
|
const parsed = Number(retryAfterHeader)
|
|
if (Number.isFinite(parsed) && parsed > 0) retryAfter = parsed
|
|
}
|
|
|
|
throw classifyResponseError({
|
|
message: `Failed to fetch status page: ${response.statusText}`,
|
|
code: response.status,
|
|
retryAfter,
|
|
})
|
|
}
|
|
|
|
const json = await response.json()
|
|
return StatusPageResponseSchema.parse(json)
|
|
}
|
|
|
|
export type StatusPageData = Awaited<ReturnType<typeof getStatusPage>>
|
|
export type StatusPageError = StudioError
|
|
|
|
export const statusPageQueryOptions = () =>
|
|
queryOptions({
|
|
queryKey: platformKeys.statusPage(),
|
|
queryFn: ({ signal }) => getStatusPage(signal),
|
|
staleTime: 60_000,
|
|
refetchInterval: 5 * 60_000,
|
|
refetchOnWindowFocus: false,
|
|
retryDelay: (attemptIndex: number, error: StatusPageError) => {
|
|
if (isStudioError(error) && error.retryAfter) {
|
|
return error.retryAfter * 1000
|
|
}
|
|
return Math.min(1000 * 4 ** attemptIndex, 1000 * 60 * 5)
|
|
},
|
|
enabled: IS_PLATFORM,
|
|
})
|