mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
docs/tutorial-template
3
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
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`). |
||
|
|
d69d8e1bc1 |
fix(studio): retry empty JSON GET responses without the HTTP cache (#53089)
## Problem
Successful GET requests sometimes reach Studio as a `200` with an empty
body. The fetcher turns these into `data: {}`, which doesn't match the
generated types, so components crash on nested reads such as
`pages[0].pagination.count`, `project.ref` and `features.vectorBuckets`.
Sentry diagnostics from #51123 show:
- Nearly all of these events come from Firefox.
- A `cache: 'no-store'` refetch of the same request returns a normal
body in more than 99% of events, so the API isn't sending empty
responses.
- The API sends no `Cache-Control` and a weak ETag, so Firefox
revalidates stored entries with `If-None-Match`. A browser cache or
revalidation bug is the most likely cause, but it isn't proven.
## Solution
When a successful response has an empty body,
`normalizeEmptyBodyResponse` now tries to recover it before falling back
to `{}`:
1. If only the clone used for the peek was empty, it reads the original
response and uses that body.
2. For JSON GET requests, it re-fetches once with `cache: 'no-store'`
and a new `X-Request-Id`, using plain `fetch` so the middleware can't
recurse. The retry uses the query's abort signal. A `2xx` response with
a body is returned to the caller.
3. Otherwise it keeps today's behavior: `Content-Length: 0`, which gives
`data: {}`.
The retry only covers GET requests with an `application/json` response,
so these stay unchanged:
- empty mutation responses
- the branch diff endpoint, which legitimately returns an empty
`text/html` body when there's no diff
The retry replaces the diagnostic probe, so a failure costs two requests
instead of three. The Sentry report gets a new `recovered_by` tag
(`original_body`, `refetch` or `none`), which will show whether the fix
works and whether the clone or the cache was at fault. The existing
`probe_*` fields keep their names, and Sentry still only receives body
lengths, never bodies.
### Why not bypass the cache for every request?
We chose not to send every request with `cache: 'no-store'`. It would
remove every `304 Not Modified` response, so unchanged data would come
back as a full `200` body each time and more data would go over the
network. The retry keeps HTTP caching for normal requests and only skips
the cache for requests that already came back empty.
## How to test
- `pnpm vitest run data/empty-body-diagnostics.test.ts
data/normalizeEmptyBodyResponse.test.ts` from `apps/studio` covers:
- refetch recovery, and the secret value never reaching Sentry
- fallbacks when the retry is empty, rejects, returns non-2xx or is
aborted
- no retry for non-JSON responses or non-GET requests
- the original-body path
- After release, check the `recovered_by` tag on the "Empty response
body on successful API request" Sentry issues, and confirm the related
crash issues (`pagination is undefined`, `can't access property "ref"`)
trend down.
Part of FE-4665
Related to FE-4912
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
||
|
|
6c6b19c6c6 |
chore(studio): report empty-body GET 200s to Sentry with a no-store probe (#51123)
<!-- ccr-slack-attribution --> _Requested by **Ivan Vasilov** · [Slack thread](https://supabase.slack.com/archives/C063LNYJJKS/p1790710416069689?thread_ts=1790710416.069689&cid=C063LNYJJKS)_ **Before:** When a Studio API GET comes back as a 200 with an empty body, openapi-fetch hands the caller `{}` and we only see the downstream crash, with no record of the response that caused it. **After:** The first time this happens for an endpoint in a page session, Studio sends one Sentry warning, `Empty response body on successful API request`. It carries the response metadata, browser state, resource timing, and the result of a single `cache: 'no-store'` refetch. What the caller receives is unchanged. ## Problem Studio crashes trace back to GET requests that return 200 with an empty body, which openapi-fetch turns into `{}`. They are heavily skewed to Firefox and Safari. The leading hypothesis is browser cache revalidation (Express weak ETags, no `Cache-Control` on api.supabase.com), but nothing confirms it yet. The `no-store` probe tells the two cases apart: if the refetch has a body, the browser cache is the likely culprit; if it is also empty, the server or the edge is sending empty bodies. This data should show whether the fix belongs on the API side or the Cloudflare side. Context: #51041 (closed) tried to guard the crashing call sites instead. ## Needs API-side change to be fully useful Cross-origin, Studio can only read CORS-safelisted response headers, and resource timing sizes read as zero. If api.supabase.com sends `Access-Control-Expose-Headers: ETag, cf-ray, cf-cache-status, x-request-id` and `Timing-Allow-Origin: <studio origin>`, this event will also carry the ETag, cf-ray, and cache status, plus the real transfer and body sizes and the negotiated protocol. Until then, those fields read as `null` or `0`. ## Solution - `data/empty-body-diagnostics.ts` (new): `reportEmptyBodyResponse({ request, response, schemaPath })`. - Runs only for `GET` and only when `IS_PLATFORM`. Empty POST/201 bodies are legitimate. - Reports at most once per templated endpoint per page session (module-level `Set`). - Endpoint: openapi-fetch's `schemaPath` (e.g. `/platform/projects/{ref}/settings`), passed through `templateEndpointPath`. That function drops the query string and hash, replaces the segment after `projects`/`organizations`/`branches` with `{ref}`/`{slug}`/`{branch}`, and replaces UUIDs, numeric IDs, and 20+ character alphanumeric IDs with `{id}`. I used `schemaPath` rather than the request URL so user-chosen names (bucket names, function slugs) never end up in tags or fingerprints. - Probe: one plain `fetch(new Request(request, { cache: 'no-store', ... }))` with a fresh `X-Request-Id` and a 10s `AbortController` timeout. `AbortSignal.timeout` isn't available in older Safari. The probe bypasses the openapi-fetch middleware, so it can't recurse. Only the body's byte length is recorded, never its contents. - Event: `level: 'warning'`, `fingerprint: ['empty-body-response', endpoint]`, `tags: { endpoint, probe_has_body, empty_body_diagnostic: 'true' }`, where `probe_has_body` is `true` / `false` / `error`. `extra` holds: - the request: method, status, `response.type`, `redirected`, and the original `X-Request-Id` (for API log lookup) - response headers: `content-type`, `cache-control`, `last-modified`, `expires`, `content-length`, `etag`, `cf-ray`, `cf-cache-status`, `x-request-id` - browser state: `visibilityState`, `navigator.onLine`, the navigation type, ms since navigation start, and whether the page was restored from bfcache - the latest `PerformanceResourceTiming` for the URL (transfer, encoded, and decoded size, `nextHopProtocol`, `responseStatus`) - the probe: status, request ID, body length, `content-length`, `content-type`, or the error name - Fire-and-forget: everything is wrapped in a `try`/`catch`, and the caller does not await it. - `data/fetchers.ts`: the `onResponse` middleware passes `{ request, schemaPath }` to `normalizeEmptyBodyResponse`, which calls the reporter in its empty-body branch and also for a 200 that carries `Content-Length: 0`. openapi-fetch short-circuits that case to `{}` the same way, so it is the same symptom. The return value is unchanged in every branch. - `packages/common/sentry.ts`: `filterSentryEvent` normally keeps only 1% of events that aren't page crashes. It now sends events tagged `empty_body_diagnostic` unsampled, with `codeSampleRate: '1'`. A once-per-session warning would barely show up at 1%. Consent and platform gating and the third-party filter still apply. www and docs also use `filterSentryEvent`, but only Studio's reporter sets this tag, so sampling for them and for every other Studio event is unchanged. Sentry config: Studio's `beforeSend` doesn't otherwise drop this message. It has no exception values, so the no-stack-trace filter doesn't apply, and it matches no `ignoreErrors` entry. ## Review instructions 1. Check `normalizeEmptyBodyResponse` in `data/fetchers.ts`: the reporter is `void`-called and its return value is untouched. 2. Check `probe()` in `data/empty-body-diagnostics.ts`: only `byteLength` is read from the body. The probe reuses the original request's headers and credentials (same auth as the original GET). 3. Check `filterSentryEvent` in `packages/common/sentry.ts`: only the `empty_body_diagnostic` tag skips sampling. 4. Tests: `data/empty-body-diagnostics.test.ts` and `packages/common/sentry.test.ts`. ## Verification - Unit tests (`data/empty-body-diagnostics.test.ts`, new): - path templating cases - `probe_has_body` `true` / `false` / `error` - the secret body content never appears in the Sentry call - one report per endpoint - non-GET and non-platform requests are skipped - no throw when `fetch` or Sentry throws - end-to-end through `client.GET`: still resolves `{}` and reports the `schemaPath`, for both a missing `Content-Length` and `Content-Length: 0` - `packages/common/sentry.test.ts`: tagged diagnostics are sent unsampled, untagged or false-tagged ones are still sampled, and they're still dropped without consent. These tests, plus the existing `normalizeEmptyBodyResponse.test.ts`, `handleError.test.ts`, and the rest of `sentry.test.ts`, pass (67 tests) under vitest 5 + jsdom. I ran them in a minimal harness, not the full `pnpm install` workspace, because the local checkout is sparse. - I ran TypeScript 7.0.2 (`--strict`) on the five touched files against the real `api-types`, with stubbed `common`/Sentry types. No errors in the touched files. - Prettier `--check` with the repo config passes, with and without `SORT_IMPORTS=false`. - Not run locally: the full studio typecheck, `lint:ratchet`, and knip. CI covers them. The change adds no `any`, no default exports, and no deps. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01UefDak8XYLMi9aiEjDPXc5 --------- Co-authored-by: Claude <noreply@anthropic.com> |