mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem
A 401 from the platform API means the browser sent no valid session
token: the user signed out, or the session expired. Studio then
redirects to sign-in. `classifyResponseError` turns any 4xx into
`UnclassifiedApiError` with `fault: 'studio'`, so every query that fails
while the session is gone goes to Sentry at the Studio-owned sample
rate. The first query to fail is often the read-replicas query. Sentry
fills with errors that Studio can't fix.
## Solution
- `UnclassifiedApiError` now derives its fault from the status: a 401 is
`user`, and every other 4xx stays `studio`.
- `ERROR_ROUTING.UnclassifiedApiError` gets a `user` entry at
`SAMPLE_USER_FAULT`. 401s still send a small trickle to catch
misclassification, and 403/404/429 keep `SAMPLE_OWNED`.
- Because the fault is `user`, `AlertError` shows the user-fault
presentation for a 401 ("try again / refresh"), with no contact-support
prompt.
- Tests: `handleError.test.ts` expects `user` for a 401, and
`error-routing.test.ts` covers the 401 and 403 routing.
## Review instructions
This change has no new UI. To check it:
1. In `apps/studio`, run `vitest run lib/errors data/handleError.test.ts
data/error-classification.test.ts data/fetchers-errors.test.ts
data/query-client-reporting.test.ts components/ui`. All tests pass.
2. In `apps/studio`, run `eslint` on the changed files. It reports
nothing.
3. `tsc --noEmit` in `apps/studio` reports one error, in
`packages/ui-patterns/src/McpUrlBuilder/components/InstructionBlocks.tsx`.
The same error appears without this change: it comes from running `tsc`
without `next typegen` first.
[FE-5086](https://linear.app/supabase/issue/FE-5086/unclassifiedapierror-no-access-token-provided)
Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
227 lines
8.1 KiB
TypeScript
227 lines
8.1 KiB
TypeScript
import { Data } from 'effect'
|
|
|
|
import type { ErrorMetadata } from '@/types/base'
|
|
|
|
export type Fault = 'user' | 'platform' | 'third_party' | 'studio'
|
|
export type ClassifiedBy = 'rule' | 'default'
|
|
export type StudioService = 'management-api' | 'pg-meta' | 'unknown'
|
|
|
|
export const DEFAULT_ERROR_MESSAGE =
|
|
'API error happened while trying to communicate with the server.'
|
|
|
|
export type StudioErrorInput = {
|
|
message?: string
|
|
code?: number
|
|
requestId?: string
|
|
retryAfter?: number
|
|
metadata?: ErrorMetadata
|
|
formattedError?: string
|
|
service?: StudioService
|
|
/** OpenAPI path pattern, never a concrete URL */
|
|
endpoint?: string
|
|
method?: string
|
|
/** Static name of the Studio query that ran the SQL, passed to `executeSql` */
|
|
queryName?: string
|
|
/** Postgres SQLSTATE code (e.g. `42702`), for SQL errors */
|
|
sqlState?: string
|
|
/** 1-based character offset of the Postgres error in the SQL, for SQL errors */
|
|
position?: string
|
|
cause?: unknown
|
|
}
|
|
|
|
/** What a class's constructor takes: the shared fields plus its own reasons. */
|
|
type StudioErrorInputFor<R extends string> = StudioErrorInput & { reason?: R }
|
|
|
|
type StudioErrorFields<R extends string> = Omit<StudioErrorInput, 'message' | 'service'> & {
|
|
readonly message: string
|
|
readonly reason: R
|
|
readonly service: StudioService
|
|
}
|
|
|
|
type StudioErrorConfig<F extends Fault, C extends ClassifiedBy, R extends string> = {
|
|
fault: F | ((input: StudioErrorInputFor<R>) => F)
|
|
classifiedBy: C
|
|
/** The reasons this class can have, so a mistyped one fails typecheck. The first is the default. */
|
|
reasons: readonly [R, ...R[]]
|
|
retryable: (input: StudioErrorInputFor<R>) => boolean
|
|
/**
|
|
* What Sentry shows instead of `message`, for classes whose message can contain user data. Built
|
|
* only from fields that can't. `message` stays intact for the UI.
|
|
*/
|
|
sentryMessage?: (input: StudioErrorInputFor<R>) => string
|
|
}
|
|
|
|
export function getServiceForEndpoint(endpoint?: string): StudioService {
|
|
if (!endpoint) return 'unknown'
|
|
if (endpoint.startsWith('/platform/pg-meta/')) return 'pg-meta'
|
|
if (endpoint.startsWith('/v1/')) return 'management-api'
|
|
if (endpoint.startsWith('/platform/')) return 'management-api'
|
|
return 'unknown'
|
|
}
|
|
|
|
function studioError<
|
|
const Tag extends string,
|
|
F extends Fault,
|
|
C extends ClassifiedBy,
|
|
R extends string,
|
|
>(tag: Tag, config: StudioErrorConfig<F, C, R>) {
|
|
return class extends Data.TaggedError(tag)<StudioErrorFields<R>> {
|
|
readonly fault: F
|
|
readonly classifiedBy: C = config.classifiedBy
|
|
readonly retryable: boolean
|
|
/** The message sent to Sentry. Equal to `message` unless the class redacts it. */
|
|
readonly sentryMessage: string
|
|
/** Set by `reportStudioError` when the error is sent to Sentry. Assigned before `beforeSend`, so it may refer to an event that was sampled out. */
|
|
errorEventId?: string
|
|
|
|
constructor(input: StudioErrorInputFor<R> = {}) {
|
|
super({
|
|
...input,
|
|
message: input.message || DEFAULT_ERROR_MESSAGE,
|
|
reason: input.reason ?? config.reasons[0],
|
|
service: input.service ?? getServiceForEndpoint(input.endpoint),
|
|
})
|
|
this.fault = typeof config.fault === 'function' ? config.fault(input) : config.fault
|
|
this.retryable = config.retryable(input)
|
|
this.sentryMessage = config.sentryMessage?.(input) ?? this.message
|
|
}
|
|
}
|
|
}
|
|
|
|
export class RequestAbortedError extends studioError('RequestAborted', {
|
|
fault: 'user',
|
|
classifiedBy: 'rule',
|
|
reasons: ['request_aborted'],
|
|
retryable: () => false,
|
|
}) {}
|
|
|
|
// A fetch failure can't say why it failed, so it's the user's network only when the browser
|
|
// reports being offline. Otherwise it may be a gateway error without CORS headers or a dropped
|
|
// connection on our side.
|
|
export class NetworkUnreachableError extends studioError('NetworkUnreachable', {
|
|
fault: ({ reason }) => (reason === 'offline' ? 'user' : 'platform'),
|
|
classifiedBy: 'rule',
|
|
reasons: ['network_unreachable', 'offline'],
|
|
retryable: () => true,
|
|
}) {}
|
|
|
|
// An empty body where the endpoint advertises one. Body shapes are enforced by the generated
|
|
// types, not checked at runtime.
|
|
export class MalformedResponseError extends studioError('MalformedResponse', {
|
|
fault: 'platform',
|
|
classifiedBy: 'rule',
|
|
reasons: ['empty_body'],
|
|
retryable: () => true,
|
|
}) {}
|
|
|
|
export class PlatformError extends studioError('PlatformError', {
|
|
fault: 'platform',
|
|
classifiedBy: 'default',
|
|
reasons: ['server_error'],
|
|
retryable: () => true,
|
|
}) {}
|
|
|
|
// A 401 means the browser sent no valid session token (signed out, or the session expired), which
|
|
// the user fixes by signing in again. Other 4xx statuses stay Studio's until a rule refines them.
|
|
export class UnclassifiedApiError extends studioError('UnclassifiedApiError', {
|
|
fault: ({ code }) => (code === 401 ? 'user' : 'studio'),
|
|
classifiedBy: 'default',
|
|
reasons: ['client_error', 'unauthorized', 'forbidden', 'not_found', 'rate_limited'],
|
|
retryable: ({ code }) => code === 429,
|
|
}) {}
|
|
|
|
// The cause can be the user's workload, Supabase infrastructure, or rarely Studio, and the message
|
|
// can't tell them apart, so default to platform until project health signals can refine it.
|
|
export class ConnectionTimeoutError extends studioError('DbConnectionTimeout', {
|
|
fault: 'platform',
|
|
classifiedBy: 'rule',
|
|
reasons: ['connection_timeout'],
|
|
retryable: () => true,
|
|
}) {}
|
|
|
|
// Postgres rejected SQL the user (or an AI agent acting for them) wrote. The message can echo
|
|
// the user's SQL and data, so this error is never sent to Sentry (see `ERROR_ROUTING` and
|
|
// `isNeverReportedError`).
|
|
export class UserSqlError extends studioError('UserSqlError', {
|
|
fault: 'user',
|
|
classifiedBy: 'rule',
|
|
reasons: ['user_sql'],
|
|
retryable: () => false,
|
|
}) {}
|
|
|
|
// Postgres rejected SQL that Studio wrote. The Postgres message can still echo user data (row
|
|
// values in a constraint violation, a schema name), so Sentry gets only the SQLSTATE and the query
|
|
// name. When the SQL embeds user input and the caller's `isUserFaultSqlState` accepts the SQLSTATE,
|
|
// the reason is `user_input` and the fault is the user's, which gives it a lower Sentry sample rate (see
|
|
// `getErrorRouting`).
|
|
export class StudioSqlError extends studioError('StudioSqlError', {
|
|
fault: ({ reason }) => (reason === 'user_input' ? 'user' : 'studio'),
|
|
classifiedBy: 'rule',
|
|
reasons: ['studio_sql', 'user_input'],
|
|
retryable: () => false,
|
|
sentryMessage: ({ sqlState, queryName }) =>
|
|
`Postgres error ${sqlState ?? 'without SQLSTATE'} in query ${queryName ?? 'without name'}`,
|
|
}) {}
|
|
|
|
export class UnexpectedError extends studioError('UnexpectedError', {
|
|
fault: 'studio',
|
|
classifiedBy: 'default',
|
|
reasons: ['unexpected'],
|
|
retryable: () => false,
|
|
}) {}
|
|
|
|
export type StudioError =
|
|
| RequestAbortedError
|
|
| NetworkUnreachableError
|
|
| MalformedResponseError
|
|
| PlatformError
|
|
| UnclassifiedApiError
|
|
| ConnectionTimeoutError
|
|
| UserSqlError
|
|
| StudioSqlError
|
|
| UnexpectedError
|
|
|
|
export type StudioErrorTag = StudioError['_tag']
|
|
export type StudioErrorReason = StudioError['reason']
|
|
|
|
export type ClassifiedError = ConnectionTimeoutError
|
|
|
|
// A Record (not a Set) so a new tag fails typecheck until it is listed here.
|
|
const STUDIO_ERROR_TAGS: Record<StudioErrorTag, true> = {
|
|
RequestAborted: true,
|
|
NetworkUnreachable: true,
|
|
MalformedResponse: true,
|
|
PlatformError: true,
|
|
UnclassifiedApiError: true,
|
|
DbConnectionTimeout: true,
|
|
UserSqlError: true,
|
|
StudioSqlError: true,
|
|
UnexpectedError: true,
|
|
}
|
|
|
|
export function isStudioError(error: unknown): error is StudioError {
|
|
return (
|
|
typeof error === 'object' &&
|
|
error !== null &&
|
|
'_tag' in error &&
|
|
typeof error._tag === 'string' &&
|
|
Object.hasOwn(STUDIO_ERROR_TAGS, error._tag)
|
|
)
|
|
}
|
|
|
|
/**
|
|
* True for errors whose fault is the user's, so the user can fix them. Says nothing about whether
|
|
* the error is reported; use `isNeverReportedError` for that.
|
|
*/
|
|
export function isUserError(error: unknown): error is StudioError {
|
|
return isStudioError(error) && error.fault === 'user'
|
|
}
|
|
|
|
/**
|
|
* True for errors that must never reach Sentry, even with `alwaysCapture` or when a third-party
|
|
* integration captures them. `beforeSend` drops them as a backstop.
|
|
*/
|
|
export function isNeverReportedError(error: unknown): boolean {
|
|
return isStudioError(error) && error._tag === 'UserSqlError'
|
|
}
|