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.
136 lines
4.4 KiB
TypeScript
136 lines
4.4 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
import { ERROR_PATTERNS } from './error-patterns'
|
|
import {
|
|
isStudioError,
|
|
NetworkUnreachableError,
|
|
PlatformError,
|
|
RequestAbortedError,
|
|
UnclassifiedApiError,
|
|
UnexpectedError,
|
|
type StudioError,
|
|
type StudioErrorInput,
|
|
} from '@/lib/errors/studio-errors'
|
|
import type { ErrorMetadata } from '@/types/base'
|
|
|
|
export type RawResponseError = Omit<StudioErrorInput, 'reason' | 'service'>
|
|
|
|
const CLIENT_ERROR_REASONS: Record<number, string> = {
|
|
401: 'unauthorized',
|
|
403: 'forbidden',
|
|
404: 'not_found',
|
|
429: 'rate_limited',
|
|
}
|
|
|
|
/** Applies the pattern rules first, then the default rules by HTTP status. */
|
|
export function classifyResponseError(raw: RawResponseError): StudioError {
|
|
const { message, code } = raw
|
|
|
|
if (message) {
|
|
const matched = ERROR_PATTERNS.find(({ pattern }) => pattern.test(message))
|
|
if (matched) return new matched.ErrorClass(raw)
|
|
}
|
|
|
|
if (code !== undefined && code >= 500)
|
|
return new PlatformError({ ...raw, reason: 'server_error' })
|
|
if (code !== undefined && code >= 400) {
|
|
return new UnclassifiedApiError({
|
|
...raw,
|
|
reason: CLIENT_ERROR_REASONS[code] ?? 'client_error',
|
|
})
|
|
}
|
|
return new UnexpectedError(raw)
|
|
}
|
|
|
|
// fetch rejects with a DOMException, which is not an Error subclass in every runtime
|
|
export function isAbortError(err: unknown): err is { name: 'AbortError'; message: string } {
|
|
return typeof err === 'object' && err !== null && 'name' in err && err.name === 'AbortError'
|
|
}
|
|
|
|
// Browsers reject fetch with a TypeError on network failure, but TypeError also covers
|
|
// ordinary programming bugs, so match only the fetch-failure messages (Chrome, Firefox, Safari)
|
|
const FETCH_FAILURE_MESSAGES = [
|
|
'Failed to fetch',
|
|
'NetworkError when attempting to fetch resource',
|
|
'Load failed',
|
|
'Network request failed',
|
|
]
|
|
|
|
export function isFetchFailure(err: unknown): err is TypeError {
|
|
return (
|
|
err instanceof TypeError &&
|
|
FETCH_FAILURE_MESSAGES.some((message) => err.message.startsWith(message))
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Classifies anything that isn't an API response: an already-tagged error, an abort, a browser
|
|
* fetch failure, or something unrecognized. It knows nothing about API bodies; for those, use
|
|
* `classifyResponseError`.
|
|
*/
|
|
export function classifyError(
|
|
error: unknown,
|
|
context?: Pick<StudioErrorInput, 'endpoint' | 'method' | 'requestPathname'>
|
|
): StudioError {
|
|
if (isStudioError(error)) return error
|
|
|
|
if (isAbortError(error)) {
|
|
return new RequestAbortedError({ ...context, message: error.message, cause: error })
|
|
}
|
|
|
|
if (isFetchFailure(error)) {
|
|
const isOffline = typeof navigator !== 'undefined' && navigator.onLine === false
|
|
return new NetworkUnreachableError({
|
|
...context,
|
|
message: error.message,
|
|
reason: isOffline ? 'offline' : undefined,
|
|
cause: error,
|
|
})
|
|
}
|
|
|
|
return new UnexpectedError({
|
|
...context,
|
|
message: error instanceof Error ? error.message : undefined,
|
|
cause: error,
|
|
})
|
|
}
|
|
|
|
// Each field falls back to undefined on a wrong type, so one bad field doesn't discard the body.
|
|
const optional = <T extends z.ZodTypeAny>(schema: T) => schema.optional().catch(undefined)
|
|
|
|
const responseErrorBodySchema = z.object({
|
|
msg: optional(z.string()),
|
|
message: optional(z.string()),
|
|
code: optional(z.number()),
|
|
requestId: optional(z.string()),
|
|
retryAfter: optional(z.number()),
|
|
requestPathname: optional(z.string()),
|
|
metadata: optional(z.record(z.string(), z.unknown())),
|
|
formattedError: optional(z.string()),
|
|
})
|
|
|
|
/**
|
|
* Reads the error body the fetch middleware produces for a failed API response. Returns undefined
|
|
* when there is neither a message nor a status code, or for tagged, aborted, and fetch-failure
|
|
* errors that merely carry a message, so the caller can fall back to `classifyError`.
|
|
*/
|
|
export function parseResponseErrorBody(error: unknown): RawResponseError | undefined {
|
|
if (!error || typeof error !== 'object') return undefined
|
|
if (isStudioError(error) || isAbortError(error) || isFetchFailure(error)) return undefined
|
|
|
|
const result = responseErrorBodySchema.safeParse(error)
|
|
if (!result.success) return undefined
|
|
|
|
const { msg, message, code, metadata, ...rest } = result.data
|
|
const resolvedMessage = msg ?? message
|
|
if (!resolvedMessage && code === undefined) return undefined
|
|
|
|
return {
|
|
...rest,
|
|
message: resolvedMessage,
|
|
code,
|
|
metadata: metadata as ErrorMetadata | undefined,
|
|
cause: error instanceof Error ? error : undefined,
|
|
}
|
|
}
|