mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +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.
66 lines
2.5 KiB
TypeScript
66 lines
2.5 KiB
TypeScript
import * as Sentry from '@sentry/nextjs'
|
|
|
|
import { ERROR_ROUTING, SAMPLE_ALWAYS, SAMPLE_NEVER } from './error-routing'
|
|
import { classifyError } from '@/data/error-classification'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
|
|
export type StudioSentryContext = {
|
|
tags?: Record<string, string | number | boolean>
|
|
extra?: Record<string, unknown>
|
|
contexts?: Record<string, Record<string, unknown>>
|
|
}
|
|
|
|
/**
|
|
* Sends the error to Sentry when its routing sample rate is above zero. The rate is attached as
|
|
* the `sample_rate` tag; `beforeSend` applies it. Records the event id on the error.
|
|
* `force` is for callers that explicitly asked for capture (`alwaysCapture`) and overrides routing.
|
|
*/
|
|
export function reportStudioError(
|
|
err: StudioError,
|
|
context?: StudioSentryContext,
|
|
{ force = false }: { force?: boolean } = {}
|
|
) {
|
|
const sampleRate = force ? SAMPLE_ALWAYS : ERROR_ROUTING[err._tag].sentrySampleRate
|
|
if (sampleRate <= SAMPLE_NEVER) return
|
|
if (err.errorEventId) return
|
|
|
|
const eventId = Sentry.captureException(err, {
|
|
...context,
|
|
tags: {
|
|
...context?.tags,
|
|
error_tag: err._tag,
|
|
sample_rate: sampleRate,
|
|
fault: err.fault,
|
|
classified_by: err.classifiedBy,
|
|
service: err.service,
|
|
...(err.endpoint && { endpoint: err.endpoint }),
|
|
},
|
|
extra: { ...context?.extra, requestId: err.requestId, status: err.code },
|
|
})
|
|
err.errorEventId = eventId
|
|
}
|
|
|
|
type DeferredReportOptions = { context?: StudioSentryContext; force?: boolean }
|
|
|
|
// Kept off the error so its fields describe only the error, not how the caller wants it reported.
|
|
const deferredReportOptions = new WeakMap<StudioError, DeferredReportOptions>()
|
|
|
|
/**
|
|
* Remembers the reporting options a caller passed to `handleError`, keyed by the error instance, so
|
|
* the query cache can report with them after the last retry (see `reportQueryError`).
|
|
*/
|
|
export function deferStudioErrorReport(err: StudioError, options: DeferredReportOptions) {
|
|
deferredReportOptions.set(err, options)
|
|
}
|
|
|
|
/**
|
|
* Reports an error that exhausted its React Query retries. Called from the `QueryCache` and
|
|
* `MutationCache` `onError`. Errors thrown by `handleError` are already classified; anything else
|
|
* (e.g. a query function that throws its own `Error`) is classified here.
|
|
*/
|
|
export function reportQueryError(error: unknown) {
|
|
const classified = classifyError(error)
|
|
const options = deferredReportOptions.get(classified)
|
|
reportStudioError(classified, options?.context, { force: options?.force })
|
|
}
|