mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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.
210 lines
6.6 KiB
TypeScript
210 lines
6.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
COMPUTE_INSTANCE_NAME_WORDS,
|
|
computeInstanceUrl,
|
|
getComputeInstanceStateMeta,
|
|
} from './Compute.constants'
|
|
import type { ComputeInstance } from './Compute.types'
|
|
import {
|
|
filterComputeInstances,
|
|
formatResources,
|
|
formatRuntime,
|
|
formatSize,
|
|
generateComputeInstanceName,
|
|
getPage,
|
|
isComputeForbidden,
|
|
isComputeUnavailable,
|
|
} from './Compute.utils'
|
|
import { classifyResponseError } from '@/data/error-classification'
|
|
|
|
const instance = (
|
|
overrides: Partial<ComputeInstance> & Pick<ComputeInstance, 'name'>
|
|
): ComputeInstance => ({
|
|
buildState: 'active',
|
|
isDeleting: false,
|
|
runtime: 'node',
|
|
size: '2gb-1vcpu',
|
|
access: 'public',
|
|
declaredInstances: 1,
|
|
...overrides,
|
|
})
|
|
|
|
const NO_FILTERS = { search: '', state: 'all', access: 'all' } as const
|
|
|
|
describe('filterComputeInstances', () => {
|
|
const instances = [
|
|
instance({ name: 'embed', buildState: 'active', access: 'public' }),
|
|
instance({ name: 'resize-images', buildState: 'building', access: 'private' }),
|
|
instance({ name: 'embed-batch', buildState: 'failed', access: 'public' }),
|
|
]
|
|
|
|
it('returns every instance when no filters are set', () => {
|
|
expect(filterComputeInstances(instances, NO_FILTERS)).toHaveLength(3)
|
|
})
|
|
|
|
it('matches names case-insensitively on a partial term', () => {
|
|
const names = filterComputeInstances(instances, { ...NO_FILTERS, search: 'EMBED' }).map(
|
|
(i) => i.name
|
|
)
|
|
expect(names).toEqual(['embed', 'embed-batch'])
|
|
})
|
|
|
|
it('ignores surrounding whitespace in the search term', () => {
|
|
expect(filterComputeInstances(instances, { ...NO_FILTERS, search: ' resize ' })).toHaveLength(
|
|
1
|
|
)
|
|
})
|
|
|
|
it('filters by build state and by access', () => {
|
|
expect(
|
|
filterComputeInstances(instances, { ...NO_FILTERS, state: 'failed' }).map((i) => i.name)
|
|
).toEqual(['embed-batch'])
|
|
expect(
|
|
filterComputeInstances(instances, { ...NO_FILTERS, access: 'private' }).map((i) => i.name)
|
|
).toEqual(['resize-images'])
|
|
})
|
|
|
|
it('combines filters', () => {
|
|
expect(
|
|
filterComputeInstances(instances, { search: 'embed', state: 'active', access: 'public' })
|
|
).toEqual([instances[0]])
|
|
})
|
|
})
|
|
|
|
describe('getPage', () => {
|
|
const items = Array.from({ length: 25 }, (_, index) => index)
|
|
|
|
it('returns the first window and the total page count', () => {
|
|
const page = getPage(items, 1, 10)
|
|
expect(page.items).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
|
expect(page).toMatchObject({ currentPage: 1, totalPages: 3, startIndex: 0 })
|
|
})
|
|
|
|
it('returns the remainder on the last page', () => {
|
|
expect(getPage(items, 3, 10).items).toEqual([20, 21, 22, 23, 24])
|
|
})
|
|
|
|
it('clamps a page beyond the end so filtering never strands an empty page', () => {
|
|
const page = getPage(items.slice(0, 5), 3, 10)
|
|
expect(page.currentPage).toBe(1)
|
|
expect(page.items).toHaveLength(5)
|
|
})
|
|
|
|
it('reports a single page when there are no items', () => {
|
|
expect(getPage([], 1, 10)).toEqual({
|
|
items: [],
|
|
currentPage: 1,
|
|
totalPages: 1,
|
|
startIndex: 0,
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('formatSize', () => {
|
|
it('splits the memory and vCPU parts the API reports', () => {
|
|
expect(formatSize('2gb-1vcpu')).toBe('2 GB · 1 vCPU')
|
|
expect(formatSize('4gb-2vcpu')).toBe('4 GB · 2 vCPU')
|
|
})
|
|
|
|
it('falls back to the raw value for a shape it does not recognize', () => {
|
|
expect(formatSize('8gb-4vcpu-gpu')).toBe('8gb-4vcpu-gpu')
|
|
expect(formatSize('')).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('formatRuntime', () => {
|
|
it('labels a known runtime', () => {
|
|
expect(formatRuntime('python')).toBe('Python 3.14')
|
|
})
|
|
|
|
it('shows the raw value for a runtime it does not know', () => {
|
|
expect(formatRuntime('rust')).toBe('rust')
|
|
})
|
|
|
|
it('labels an omitted runtime as custom', () => {
|
|
expect(formatRuntime(undefined)).toBe('Custom')
|
|
})
|
|
})
|
|
|
|
describe('formatResources', () => {
|
|
it('combines size and declared instance count', () => {
|
|
expect(formatResources(instance({ name: 'embed', declaredInstances: 3 }))).toBe(
|
|
'2 GB · 1 vCPU · 3 inst'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('compute error classification', () => {
|
|
const responseError = (code?: number) => classifyResponseError({ message: 'Denied', code })
|
|
|
|
it('reads a 404 as the project being outside the alpha allow-list', () => {
|
|
expect(isComputeUnavailable(responseError(404))).toBe(true)
|
|
expect(isComputeForbidden(responseError(404))).toBe(false)
|
|
})
|
|
|
|
it('reads a 403 as a missing compute permission, not a missing enrollment', () => {
|
|
expect(isComputeForbidden(responseError(403))).toBe(true)
|
|
expect(isComputeUnavailable(responseError(403))).toBe(false)
|
|
})
|
|
|
|
it('leaves other failures to the generic error UI', () => {
|
|
for (const error of [responseError(500), responseError(undefined), new Error('boom'), null]) {
|
|
expect(isComputeUnavailable(error)).toBe(false)
|
|
expect(isComputeForbidden(error)).toBe(false)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('computeInstanceUrl', () => {
|
|
it('answers on the project domain alongside /compute/v1', () => {
|
|
expect(computeInstanceUrl({ endpoint: 'abcdefgh.supabase.co', name: 'embed' })).toBe(
|
|
'https://abcdefgh.supabase.co/compute/v1/embed'
|
|
)
|
|
})
|
|
|
|
it('honors a non-https protocol', () => {
|
|
expect(
|
|
computeInstanceUrl({ endpoint: 'localhost:8000', protocol: 'http', name: 'embed' })
|
|
).toBe('http://localhost:8000/compute/v1/embed')
|
|
})
|
|
|
|
it('has no url until the project settings resolve', () => {
|
|
expect(computeInstanceUrl({ endpoint: undefined, name: 'embed' })).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('generateComputeInstanceName', () => {
|
|
it('produces a name that already passes the CLI naming rules', () => {
|
|
const name = generateComputeInstanceName()
|
|
expect(name).toMatch(/^compute-[a-z]+-\d{6}$/)
|
|
expect(COMPUTE_INSTANCE_NAME_WORDS).toContain(name.split('-')[1])
|
|
})
|
|
|
|
it('varies across calls', () => {
|
|
const names = new Set(Array.from({ length: 20 }, () => generateComputeInstanceName()))
|
|
expect(names.size).toBeGreaterThan(1)
|
|
})
|
|
})
|
|
|
|
describe('getComputeInstanceStateMeta', () => {
|
|
it('labels every build state', () => {
|
|
expect(getComputeInstanceStateMeta(instance({ name: 'a', buildState: 'building' })).label).toBe(
|
|
'Building'
|
|
)
|
|
expect(getComputeInstanceStateMeta(instance({ name: 'a', buildState: 'active' })).label).toBe(
|
|
'Active'
|
|
)
|
|
expect(getComputeInstanceStateMeta(instance({ name: 'a', buildState: 'failed' })).label).toBe(
|
|
'Failed'
|
|
)
|
|
})
|
|
|
|
it('reports deleting over the build state', () => {
|
|
expect(
|
|
getComputeInstanceStateMeta(instance({ name: 'a', buildState: 'active', isDeleting: true }))
|
|
.label
|
|
).toBe('Deleting')
|
|
})
|
|
})
|