mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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.
79 lines
2.9 KiB
TypeScript
79 lines
2.9 KiB
TypeScript
import { COMPUTE_INSTANCE_NAME_WORDS, RUNTIMES, type RuntimeMeta } from './Compute.constants'
|
|
import type {
|
|
ComputeInstance,
|
|
ComputeInstanceAccess,
|
|
ComputeInstanceBuildState,
|
|
} from './Compute.types'
|
|
import { isStudioError } from '@/lib/errors/studio-errors'
|
|
|
|
export interface ComputeInstanceFilters {
|
|
search: string
|
|
state: ComputeInstanceBuildState | 'all'
|
|
access: ComputeInstanceAccess | 'all'
|
|
}
|
|
|
|
export const filterComputeInstances = (
|
|
instances: ComputeInstance[],
|
|
filters: ComputeInstanceFilters
|
|
): ComputeInstance[] => {
|
|
const search = filters.search.trim().toLowerCase()
|
|
return instances.filter((instance) => {
|
|
const matchesSearch = instance.name.toLowerCase().includes(search)
|
|
const matchesState = filters.state === 'all' || instance.buildState === filters.state
|
|
const matchesAccess = filters.access === 'all' || instance.access === filters.access
|
|
return matchesSearch && matchesState && matchesAccess
|
|
})
|
|
}
|
|
|
|
export interface Page<T> {
|
|
items: T[]
|
|
currentPage: number
|
|
totalPages: number
|
|
startIndex: number
|
|
}
|
|
|
|
// Clamps the requested page so filtering down to fewer results never strands an empty page.
|
|
export const getPage = <T>(items: T[], requestedPage: number, pageSize: number): Page<T> => {
|
|
const totalPages = Math.max(1, Math.ceil(items.length / pageSize))
|
|
const currentPage = Math.min(Math.max(1, requestedPage), totalPages)
|
|
const startIndex = (currentPage - 1) * pageSize
|
|
return {
|
|
items: items.slice(startIndex, startIndex + pageSize),
|
|
currentPage,
|
|
totalPages,
|
|
startIndex,
|
|
}
|
|
}
|
|
|
|
export const getRuntimeMeta = (runtime: string | undefined): RuntimeMeta | undefined =>
|
|
runtime === undefined ? undefined : RUNTIMES[runtime]
|
|
|
|
export const formatRuntime = (runtime: string | undefined): string =>
|
|
getRuntimeMeta(runtime)?.label ?? runtime ?? 'Custom'
|
|
|
|
// The API reports size as e.g. "2gb-1vcpu"; render the parts when they parse, the raw value if not.
|
|
export const formatSize = (size: string): string => {
|
|
const match = size.match(/^(\d+)gb-(\d+)vcpu$/)
|
|
if (!match) return size
|
|
return `${match[1]} GB · ${match[2]} vCPU`
|
|
}
|
|
|
|
export const formatResources = (instance: ComputeInstance): string =>
|
|
`${formatSize(instance.size)} · ${instance.declaredInstances} inst`
|
|
|
|
// Suggests a friendly, already-valid starting name so the deploy dialog isn't blank.
|
|
export const generateComputeInstanceName = (): string => {
|
|
const word =
|
|
COMPUTE_INSTANCE_NAME_WORDS[Math.floor(Math.random() * COMPUTE_INSTANCE_NAME_WORDS.length)]
|
|
const number = Math.floor(Math.random() * 900000) + 100000
|
|
return `compute-${word}-${number}`
|
|
}
|
|
|
|
// A project outside the alpha allow-list gets a 404, not a 403.
|
|
export const isComputeUnavailable = (error: Error | null): boolean =>
|
|
isStudioError(error) && error.code === 404
|
|
|
|
// An enrolled project still answers 403 when the caller lacks the compute permission.
|
|
export const isComputeForbidden = (error: Error | null): boolean =>
|
|
isStudioError(error) && error.code === 403
|