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.
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import * as Sentry from '@sentry/nextjs'
|
|
import type { JwtPayload } from '@supabase/supabase-js'
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { apiAuthenticate } from './apiAuthenticate'
|
|
import { apiWrapper } from './apiWrapper'
|
|
import { classifyResponseError } from '@/data/error-classification'
|
|
|
|
vi.mock('@/lib/constants', () => ({
|
|
IS_PLATFORM: true,
|
|
API_URL: 'https://api.example.com',
|
|
}))
|
|
|
|
vi.mock('./apiAuthenticate', () => ({
|
|
apiAuthenticate: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@sentry/nextjs', () => ({
|
|
captureException: vi.fn(),
|
|
}))
|
|
|
|
describe('apiWrapper', () => {
|
|
const mockReq = {} as NextApiRequest
|
|
const mockRes = {
|
|
status: vi.fn().mockReturnThis(),
|
|
json: vi.fn().mockReturnThis(),
|
|
} as unknown as NextApiResponse
|
|
const mockHandler = vi.fn()
|
|
|
|
it('should call handler directly when withAuth is false', async () => {
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
|
|
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, undefined)
|
|
expect(apiAuthenticate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should pass JWT claims to handler when withAuth is true', async () => {
|
|
const mockClaims: JwtPayload = {
|
|
iss: 'supabase',
|
|
sub: 'user-123',
|
|
aud: 'authenticated',
|
|
exp: 9999999999,
|
|
iat: 1000000000,
|
|
role: 'authenticated',
|
|
aal: 'aal1',
|
|
session_id: 'session-123',
|
|
}
|
|
vi.mocked(apiAuthenticate).mockResolvedValue(mockClaims)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
|
|
expect(apiAuthenticate).toHaveBeenCalledWith(mockReq, mockRes)
|
|
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, mockClaims)
|
|
})
|
|
|
|
it('should return 401 when authentication fails', async () => {
|
|
const mockError = { error: classifyResponseError({ message: 'Invalid token' }) }
|
|
vi.mocked(apiAuthenticate).mockResolvedValue(mockError)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
|
|
expect(mockRes.status).toHaveBeenCalledWith(401)
|
|
expect(mockHandler).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should report to Sentry and return 500 when the handler throws', async () => {
|
|
const mockError = new Error('boom')
|
|
mockHandler.mockRejectedValue(mockError)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
|
|
expect(Sentry.captureException).toHaveBeenCalledWith(mockError)
|
|
expect(mockRes.status).toHaveBeenCalledWith(500)
|
|
expect(mockRes.json).toHaveBeenCalledWith({ error: mockError })
|
|
})
|
|
})
|