mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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.
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import type { JwtPayload } from '@supabase/supabase-js'
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { getUserClaims } from '@/lib/gotrue'
|
|
|
|
/**
|
|
* Use this method on api routes to check if user is authenticated and having required permissions.
|
|
* This method can only be used from the server side.
|
|
* Member permission is mandatory whenever orgSlug/projectRef query param exists
|
|
* @param {NextApiRequest} req
|
|
* @param {NextApiResponse} _res
|
|
*
|
|
* @returns {Object<user, error, description>}
|
|
* user null, with error and description if not authenticated or not enough permissions
|
|
*/
|
|
export async function apiAuthenticate(
|
|
req: NextApiRequest,
|
|
_res: NextApiResponse
|
|
): Promise<JwtPayload | { error: Error }> {
|
|
try {
|
|
const claims = await fetchUserClaims(req)
|
|
if (!claims) {
|
|
return { error: new Error('The user does not exist') }
|
|
}
|
|
|
|
return claims
|
|
} catch (error) {
|
|
return { error: error as Error }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @returns
|
|
* user with only id prop or detail object. It depends on requireUserDetail config
|
|
*/
|
|
export async function fetchUserClaims(req: NextApiRequest): Promise<JwtPayload> {
|
|
const token = req.headers.authorization?.replace(/bearer /i, '')
|
|
if (!token) {
|
|
throw new Error('missing access token')
|
|
}
|
|
const { claims, error } = await getUserClaims(token)
|
|
if (error) {
|
|
throw error
|
|
}
|
|
|
|
if (!claims) {
|
|
throw new Error('The user does not exist')
|
|
}
|
|
|
|
return claims
|
|
}
|