mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +00:00
## Problem Follow-up to #52730. Resolves part of FE-4772. Fetch failures and aborts were classified as `UnexpectedError`, and HTTP error responses with empty, HTML, or plain-text bodies did too. ## Solution **Network failures and aborts keep their classification.** `fetchHandler` used to rewrite `Failed to fetch` into a plain `Error`, so openapi-fetch network failures were classified as `UnexpectedError`. It now throws `classifyError(err, { endpoint, method })`, giving `NetworkUnreachable` (same user-facing message, original error as `cause`) or `RequestAborted`. The endpoint comes from `schemaPath`, recorded in `onRequest`. Tagged aborts are no longer named `AbortError`, so `isRequestAborted()` checks for both. `state/storage-explorer.tsx` uses it, so cancelling a folder load no longer shows an error toast. **One place reads error responses.** `readResponseError` in `data/error-classification.ts` turns an HTTP error response into a `RawResponseError`. It reads the status, `Retry-After`, the message from a JSON body or a short `text/plain` body (never HTML), plus `requestId`, `endpoint`, and `method`. The openapi-fetch middleware serializes its result into the error body. openapi-fetch builds `error` only from the response body, so that step can't be skipped. `parseResponseErrorBody` now only checks the shape. Empty, HTML, and plain-text error bodies now classify by status. Before, they became `UnexpectedError`. `fetchGet`/`fetchPost`/`fetchHeadWithTimeout` (Studio's own `/api` routes) use the same helper. This also fixes a non-JSON error body making them reject with a `SyntaxError`. **`requestPathname` is replaced by `endpoint`.** The no-retry list and the statement-timeout check in `query-client.ts` match OpenAPI patterns exactly. Critical error messages in `lib/error-reporting.ts` use the pattern, so concrete project refs stay out of them. `path-to-regexp` is no longer used, so it's removed. ## Review instructions 1. From `apps/studio`, run `vitest run data lib components/interfaces/ErrorHandling state`. Expect 4,034 passing tests. 2. Review `readResponseError` in `data/error-classification.ts` and its use in `data/fetchers.ts`. ## Additional context ### Notes - Critical error messages now read `endpoint … w/ message: …`. This regroups those Sentry issues. - A JSON body's `error` string is now accepted as a message for API responses. Before, only the `/api` route path read it. ### Testing - New `data/fetchers-errors.test.ts` covers network failure, abort, empty/HTML/plain-text/JSON error bodies, service derivation, and `/api` route non-JSON bodies. - Unit tests for `readResponseError`, `isRequestAborted`, and the no-retry endpoint set. - `vitest run data lib components/interfaces/ErrorHandling state`: 4,034 pass. ## Checklist Check all before review: - [ ] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
307 lines
10 KiB
TypeScript
307 lines
10 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
import { ERROR_PATTERNS } from './error-patterns'
|
|
import {
|
|
isStudioError,
|
|
NetworkUnreachableError,
|
|
PlatformError,
|
|
RequestAbortedError,
|
|
StudioSqlError,
|
|
UnclassifiedApiError,
|
|
UnexpectedError,
|
|
UserSqlError,
|
|
type StudioError,
|
|
type StudioErrorInput,
|
|
} from '@/lib/errors/studio-errors'
|
|
import type { ErrorMetadata } from '@/types/base'
|
|
|
|
export type RawResponseError = Omit<StudioErrorInput, 'reason' | 'service'>
|
|
|
|
const CLIENT_ERROR_REASONS: Record<number, string> = {
|
|
401: 'unauthorized',
|
|
403: 'forbidden',
|
|
404: 'not_found',
|
|
429: 'rate_limited',
|
|
}
|
|
|
|
/** Applies the pattern rules first, then the default rules by HTTP status. */
|
|
export function classifyResponseError(raw: RawResponseError): StudioError {
|
|
const { message, code } = raw
|
|
|
|
if (message) {
|
|
const matched = ERROR_PATTERNS.find(({ pattern }) => pattern.test(message))
|
|
if (matched) return new matched.ErrorClass(raw)
|
|
}
|
|
|
|
if (code !== undefined && code >= 500)
|
|
return new PlatformError({ ...raw, reason: 'server_error' })
|
|
if (code !== undefined && code >= 400) {
|
|
return new UnclassifiedApiError({
|
|
...raw,
|
|
reason: CLIENT_ERROR_REASONS[code] ?? 'client_error',
|
|
})
|
|
}
|
|
return new UnexpectedError(raw)
|
|
}
|
|
|
|
// pg-meta formats Postgres errors as `Failed to run sql query: ERROR: 42702: <message>`
|
|
const SQLSTATE_IN_MESSAGE = /\bERROR:\s+([0-9A-Z]{5}):/
|
|
|
|
/** Who wrote the SQL sent to the pg-meta query endpoint. */
|
|
export type SqlOrigin = 'user' | 'studio'
|
|
|
|
/**
|
|
* Classifies an error from the pg-meta query endpoint. Postgres rejecting the SQL comes back as a
|
|
* 400, which becomes `UserSqlError` or `StudioSqlError` depending on who wrote the SQL.
|
|
*
|
|
* A 400 whose message carries a SQLSTATE came from Postgres, so it is a SQL error before any
|
|
* message-pattern rule runs: user SQL can raise any text, including a pattern's. Other 400s go
|
|
* through the pattern rules first (a connection timeout has no SQLSTATE) and are SQL errors only
|
|
* when no rule matches. Other statuses keep their usual classification, and anything that isn't
|
|
* a response body goes through `classifyError`.
|
|
*/
|
|
export function classifySqlResponseError(
|
|
error: unknown,
|
|
{ origin, queryName }: { origin: SqlOrigin; queryName?: string }
|
|
): StudioError {
|
|
const raw = parseResponseErrorBody(error)
|
|
if (!raw) return classifyError(error)
|
|
|
|
const sqlState = raw.code === 400 ? raw.message?.match(SQLSTATE_IN_MESSAGE)?.[1] : undefined
|
|
if (!sqlState) {
|
|
const classified = classifyResponseError(raw)
|
|
if (classified._tag !== 'UnclassifiedApiError' || classified.code !== 400) return classified
|
|
}
|
|
|
|
const input = { ...raw, ...(queryName && { queryName }), ...(sqlState && { sqlState }) }
|
|
return origin === 'user' ? new UserSqlError(input) : new StudioSqlError(input)
|
|
}
|
|
|
|
// fetch rejects with a DOMException, which is not an Error subclass in every runtime
|
|
export function isAbortError(err: unknown): err is { name: 'AbortError'; message: string } {
|
|
return typeof err === 'object' && err !== null && 'name' in err && err.name === 'AbortError'
|
|
}
|
|
|
|
/**
|
|
* True for a raw abort or for the tagged `RequestAborted` error the fetch layer turns it into.
|
|
* Use this instead of checking `error.name === 'AbortError'`.
|
|
*/
|
|
export function isRequestAborted(err: unknown): boolean {
|
|
return isAbortError(err) || (isStudioError(err) && err._tag === 'RequestAborted')
|
|
}
|
|
|
|
export const NETWORK_UNREACHABLE_MESSAGE =
|
|
'Unable to reach the server. Please check your network or try again later.'
|
|
|
|
// Browsers reject fetch with a TypeError on network failure, but TypeError also covers
|
|
// ordinary programming bugs, so match only the fetch-failure messages (Chrome, Firefox, Safari)
|
|
const FETCH_FAILURE_MESSAGES = [
|
|
'Failed to fetch',
|
|
'NetworkError when attempting to fetch resource',
|
|
'Load failed',
|
|
'Network request failed',
|
|
]
|
|
|
|
export function isFetchFailure(err: unknown): err is TypeError {
|
|
return (
|
|
err instanceof TypeError &&
|
|
FETCH_FAILURE_MESSAGES.some((message) => err.message.startsWith(message))
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Classifies anything that isn't an API response: an already-tagged error, an abort, a browser
|
|
* fetch failure, or something unrecognized. It knows nothing about API bodies; for those, use
|
|
* `classifyResponseError`.
|
|
*/
|
|
export function classifyError(
|
|
error: unknown,
|
|
context?: Pick<StudioErrorInput, 'endpoint' | 'method'>
|
|
): StudioError {
|
|
if (isStudioError(error)) return error
|
|
|
|
if (isAbortError(error)) {
|
|
return new RequestAbortedError({ ...context, message: error.message, cause: error })
|
|
}
|
|
|
|
if (isFetchFailure(error)) {
|
|
const isOffline = typeof navigator !== 'undefined' && navigator.onLine === false
|
|
return new NetworkUnreachableError({
|
|
...context,
|
|
message: NETWORK_UNREACHABLE_MESSAGE,
|
|
reason: isOffline ? 'offline' : undefined,
|
|
cause: error,
|
|
})
|
|
}
|
|
|
|
return new UnexpectedError({
|
|
...context,
|
|
message: error instanceof Error ? error.message : undefined,
|
|
cause: error,
|
|
})
|
|
}
|
|
|
|
// Each field falls back to undefined on a wrong type, so one bad field doesn't discard the body.
|
|
const optional = <T extends z.ZodTypeAny>(schema: T) => schema.optional().catch(undefined)
|
|
|
|
const responseErrorBodySchema = z.object({
|
|
msg: optional(z.string()),
|
|
message: optional(z.string()),
|
|
code: optional(z.number()),
|
|
requestId: optional(z.string()),
|
|
retryAfter: optional(z.number()),
|
|
endpoint: optional(z.string()),
|
|
method: optional(z.string()),
|
|
metadata: optional(z.record(z.string(), z.unknown())),
|
|
formattedError: optional(z.string()),
|
|
position: optional(z.string()),
|
|
})
|
|
|
|
const MAX_TEXT_MESSAGE_LENGTH = 500
|
|
|
|
const jsonErrorBodySchema = z.object({
|
|
msg: optional(z.string()),
|
|
message: optional(z.string()),
|
|
error: optional(z.string()),
|
|
metadata: optional(z.record(z.string(), z.unknown())),
|
|
formattedError: optional(z.string()),
|
|
})
|
|
|
|
/** Parses `text` as JSON and returns it only when it is a plain object. */
|
|
export function parseJsonObject(text: string): Record<string, unknown> | undefined {
|
|
try {
|
|
const parsed: unknown = JSON.parse(text)
|
|
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
|
|
return parsed as Record<string, unknown>
|
|
}
|
|
} catch {
|
|
// not JSON
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
const HTTP_DATE_MONTH = /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b/
|
|
|
|
/**
|
|
* Parses a `Retry-After` header value into a number of seconds from now.
|
|
* Per RFC 9110 it is either delta-seconds (`"120"`) or an HTTP-date
|
|
* (`"Wed, 21 Oct 2015 07:28:00 GMT"`). Returns undefined for anything else.
|
|
*/
|
|
export function parseRetryAfterSeconds(
|
|
header: string | null | undefined,
|
|
now = Date.now()
|
|
): number | undefined {
|
|
const value = header?.trim()
|
|
if (!value) return undefined
|
|
if (/^\d+$/.test(value)) return Number(value)
|
|
if (!HTTP_DATE_MONTH.test(value)) return undefined
|
|
const dateMs = Date.parse(value)
|
|
if (Number.isNaN(dateMs)) return undefined
|
|
return Math.max(0, Math.ceil((dateMs - now) / 1000))
|
|
}
|
|
|
|
/**
|
|
* Parses an `X-RateLimit-Reset` header value into a number of seconds from now.
|
|
* Supabase sends seconds remaining, but many APIs send a Unix epoch timestamp
|
|
* instead. Values >= 1_000_000_000 (an epoch in seconds, after 2001) are treated
|
|
* as epochs; smaller values are treated as delta-seconds.
|
|
*/
|
|
export function parseRateLimitResetSeconds(
|
|
header: string | null | undefined,
|
|
now = Date.now()
|
|
): number | undefined {
|
|
const value = header?.trim()
|
|
if (!value || !/^\d+$/.test(value)) return undefined
|
|
const n = Number(value)
|
|
return n >= 1_000_000_000 ? Math.max(0, Math.ceil(n - now / 1000)) : n
|
|
}
|
|
|
|
/**
|
|
* The single place that turns an HTTP error response into a `RawResponseError`. A JSON object body
|
|
* supplies the message and extra fields. A short `text/plain` body becomes the message. Other
|
|
* bodies (HTML error pages, empty bodies, JSON arrays) yield no message, so classification falls
|
|
* back to the status.
|
|
*/
|
|
export function readResponseError({
|
|
status,
|
|
headers,
|
|
text,
|
|
requestId,
|
|
endpoint,
|
|
method,
|
|
}: {
|
|
status: number
|
|
headers: Headers
|
|
text: string
|
|
requestId?: string
|
|
endpoint?: string
|
|
method?: string
|
|
}): RawResponseError {
|
|
const parsed = parseJsonObject(text)
|
|
|
|
let message: string | undefined
|
|
let metadata: ErrorMetadata | undefined
|
|
let formattedError: string | undefined
|
|
|
|
if (parsed) {
|
|
const body = jsonErrorBodySchema.parse(parsed)
|
|
message = body.msg ?? body.message ?? body.error
|
|
metadata = body.metadata as ErrorMetadata | undefined
|
|
formattedError = body.formattedError
|
|
} else {
|
|
const trimmed = text.trim()
|
|
const contentType = headers.get('Content-Type') ?? ''
|
|
if (
|
|
trimmed.length > 0 &&
|
|
trimmed.length <= MAX_TEXT_MESSAGE_LENGTH &&
|
|
contentType.toLowerCase().startsWith('text/plain')
|
|
) {
|
|
message = trimmed
|
|
}
|
|
}
|
|
|
|
const retryAfter =
|
|
parseRetryAfterSeconds(headers.get('Retry-After')) ??
|
|
parseRateLimitResetSeconds(headers.get('X-RateLimit-Reset'))
|
|
|
|
const raw: RawResponseError = {
|
|
message,
|
|
code: status,
|
|
retryAfter,
|
|
requestId,
|
|
endpoint,
|
|
method,
|
|
metadata,
|
|
formattedError,
|
|
}
|
|
return Object.fromEntries(
|
|
Object.entries(raw).filter(([, value]) => value !== undefined)
|
|
) as RawResponseError
|
|
}
|
|
|
|
/**
|
|
* Validates the `RawResponseError` shape of the value `handleError` receives (a body from
|
|
* `readResponseError` or a hand-built `{ message, code }` object). Returns undefined
|
|
* when there is neither a message nor a status code, or for tagged, aborted, and fetch-failure
|
|
* errors that merely carry a message, so the caller can fall back to `classifyError`.
|
|
*/
|
|
export function parseResponseErrorBody(error: unknown): RawResponseError | undefined {
|
|
if (!error || typeof error !== 'object') return undefined
|
|
if (isStudioError(error) || isAbortError(error) || isFetchFailure(error)) return undefined
|
|
|
|
const result = responseErrorBodySchema.safeParse(error)
|
|
if (!result.success) return undefined
|
|
|
|
const { msg, message, code, metadata, ...rest } = result.data
|
|
const resolvedMessage = msg ?? message
|
|
if (!resolvedMessage && code === undefined) return undefined
|
|
|
|
return {
|
|
...rest,
|
|
message: resolvedMessage,
|
|
code,
|
|
metadata: metadata as ErrorMetadata | undefined,
|
|
cause: error instanceof Error ? error : undefined,
|
|
}
|
|
}
|