Files
supabase/apps/studio/lib/errors/error-routing.ts
T
Charis 4d28d1a809 feat(studio): throw MalformedResponse for empty JSON GET bodies (#53182)
Follows supabase/supabase#53048. Part of
[FE-4772](https://linear.app/supabase/issue/FE-4772/error-core-tagged-error-types-and-rough-fault-classification-in).

## What

`normalizeEmptyBodyResponse` already tries to recover empty success
bodies (original body, then one `no-store` refetch). When a JSON `GET`
is still empty after that, it now throws `MalformedResponseError`
(reason `empty_body`) carrying `endpoint`, `method`, `requestId` and
status, instead of resolving with `{}`, which crashes components.

* Unchanged: 204s, `HEAD`, non-`GET` requests and empty non-JSON
responses keep the `Content-Length: 0` normalization.
* Reporting: the error rejects like a network failure, so React Query
gets the tagged instance and `reportQueryError` reports it once after
the last retry. The refetch diagnostics are attached as Sentry context,
and the standalone empty-body diagnostic event is skipped for this case
to avoid double reporting. Recovered cases still send the diagnostic.
* Routing: `MalformedResponse` moves to `SAMPLE_ALWAYS`. It is rare, and
while we confirm the recovery path is sufficient every unrecovered case
should be visible.
* An abort during recovery throws `RequestAborted`, not
`MalformedResponse`

## Notes

* Callers using `get()` directly outside React Query are not reported
automatically, same as network errors.
* No GET endpoint showed up as legitimately returning an empty JSON body
in the existing empty-body diagnostics.

## Testing

* `pnpm vitest run data lib components/interfaces/ErrorHandling state`
passes.
* Lint ratchet passes; `tsc` shows only a pre-existing error in
`packages/ui-patterns` (`InstructionBlocks.tsx`).
2026-11-10 09:39:10 -05:00

42 lines
1.8 KiB
TypeScript

import type { StudioErrorTag } from '@/lib/errors/studio-errors'
/** Never send to Sentry. */
export const SAMPLE_NEVER = 0
/** Reads a 0-1 rate from an env var, falling back when it is unset or out of range. */
export function sampleRateFromEnv(value: string | undefined, fallback: number) {
if (value === undefined || value.trim() === '') return fallback
const rate = Number(value)
return Number.isFinite(rate) && rate >= 0 && rate <= 1 ? rate : fallback
}
// Env vars are read with literal `process.env.NEXT_PUBLIC_*` access so the Next and Vite builds can
// inline them.
/** Faults owned by other teams or services: keep a small sample for visibility. */
export const SAMPLE_OTHER = sampleRateFromEnv(process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE_OTHER, 0.1)
/** Faults Studio owns and fixes. */
export const SAMPLE_OWNED = sampleRateFromEnv(
process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE_OWNED,
0.25
)
/** Always send (also used when a caller forces capture). */
export const SAMPLE_ALWAYS = 1
type Routing = {
sentrySampleRate: number
// TODO: add PostHog routing when errors are sent to PostHog
}
// `satisfies Record<StudioErrorTag, ...>` makes a new tag fail typecheck until it is routed here.
export const ERROR_ROUTING = {
RequestAborted: { sentrySampleRate: SAMPLE_NEVER },
NetworkUnreachable: { sentrySampleRate: SAMPLE_OTHER },
MalformedResponse: { sentrySampleRate: SAMPLE_ALWAYS },
PlatformError: { sentrySampleRate: SAMPLE_OTHER },
UnclassifiedApiError: { sentrySampleRate: SAMPLE_OWNED },
DbConnectionTimeout: { sentrySampleRate: SAMPLE_OTHER },
UserSqlError: { sentrySampleRate: SAMPLE_NEVER },
StudioSqlError: { sentrySampleRate: SAMPLE_OWNED },
UnexpectedError: { sentrySampleRate: SAMPLE_OWNED },
} as const satisfies Record<StudioErrorTag, Routing>