6 Commits
Author SHA1 Message Date
Jordi Enric c2edb29076 fix(studio): explain Supavisor session pool limits DEBUG-43 (#53930)
## Problem

When Supavisor rejects a session because its connection pool is full,
Studio shows the error without explaining how to recover.

## Fix

Show a troubleshooting step for the session-mode limit that explains how
pool size limits concurrent clients and links to the configuration
guide.

## Screenshot

The Table Editor’s troubleshooting step, rendered locally with a sample
session-pool error.

<img width="660" alt="Expanded Supavisor session pool limit guidance in
the Table Editor"
src="https://github.com/user-attachments/assets/6072eec4-e243-4d53-a40f-30fb14d21d05"
/>

## How to test

- Display a classified MaxClientsInSessionMode error in Studio and
expand its troubleshooting step. The link should open the pool size
section of the database connection management guide.
- Display a generic transaction-mode connection limit error. It should
keep its usual fallback.
- The pattern, classification and ErrorMatcher suites exercise these
paths (144 tests passed locally).

Fixes [DEBUG-43](https://linear.app/supabase/issue/DEBUG-43).
2026-11-30 16:49:05 +01:00
supabase-vercel-tedd[bot] f0986f5f24 fix(studio): treat 401 API errors as a user fault for Sentry routing (#53411)
## 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>
2026-11-18 16:48:25 +07:00
Charis ed6d1b4c63 feat(studio): show error details in AlertError and attribute user faults in SQL errors (#53242)
## Problem

`AlertError` gives users and support no way to correlate a failure with
Sentry or platform logs. It also treats every failure the same: a bad
table editor filter or an offline browser gets "try refreshing, contact
support".

On the reporting side, SQL errors were attributed by who wrote the whole
statement. That made errors in the user's part of mixed SQL Studio's
fault: policy expressions, function bodies, cron schedules, table editor
filters and row edits. Marking them as user SQL instead would drop them
from Sentry entirely.

Part of FE-4772.

## Solution

**`AlertError`**
- Platform and Studio faults show the request ID, the Sentry event ID
(labeled "before sampling", since the event may be sampled out) and a
"Copy details" button.
- The copied text never includes `formattedError`, metadata, the cause
or SQL.
- SQL errors copy the SQLSTATE, position and query name instead of the
message.
- User faults (`isUserError`) show their message with "Please try again.
If the issue persists, try refreshing your browser." There is no support
link, no IDs and no copy button.
- Offline shows only "You may be temporarily offline. Check your
internet connection and try again."
- Defaults come from `getErrorPresentation`, and props the caller passes
still win. The table editor passes a filter-specific description when
filters are set.

**SQL error classification**
- `executeSql` takes an optional `isUserFaultSqlState` predicate for
Studio SQL. When it matches, the error is a `StudioSqlError` with reason
`user_input` and fault `user`, and is still redacted to SQLSTATE plus
query name.
- `isUserFaultForValues` (classes 22, 23 and P0, plus 42501 and 57014)
is for typed values: table editor filters and row edits, cron name and
schedule.
- `isUserFaultForSql` (any SQLSTATE except server classes 08, 53, 57
other than 57014, 58 and XX) is for embedded user SQL: policies,
functions, the custom access token hook.
- `isUserFaultForStatsStatement` is for `pg_stat_statements` text (Query
Insights `EXPLAIN`, index advisor). It treats 42P02 (`$1` placeholders)
as Studio's fault.
- `UserSqlError` is now only SQL the user wrote in full, and it is still
never sent.
- The policy, function and cron mutation hooks no longer take
`sqlOrigin`.

**Routing and types**
- `ERROR_ROUTING` is one table keyed by tag, then by fault. Its type
requires exactly the faults each class can produce.
- User faults get `SAMPLE_USER_FAULT` (default 0.01, set with
`NEXT_PUBLIC_SENTRY_SAMPLE_RATE_USER_FAULT`) to catch misclassification.
- The error class decides privacy (`isNeverReportedError`), separately
from the rate.
- Sentry events get a `reason` tag.
- Each class declares its `reasons`, so a mistyped reason fails
typecheck.

## Review instructions

1. Open the table editor, add a filter with a wrong-typed value (text on
an integer column). The alert shows the Postgres error, the filter hint,
and no support link, IDs or copy button.
2. Make a query fail with a platform error (e.g. block a `/platform/`
request in DevTools). The alert shows the request ID, the event ID and
"Copy details". Paste the copied text and check it has no SQL or
`formattedError`.
3. Go offline in DevTools and trigger a query. Only the offline message
shows.
4. Create a policy with an invalid `USING` expression. The toast shows
the Postgres error. In Sentry it is a `StudioSqlError` with reason
`user_input`, sampled at the user-fault rate.
5. Review `lib/errors/error-routing.ts` and
`data/error-classification.ts` for the routing table and the SQLSTATE
predicates.
2026-11-12 15:19:25 -05:00
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
Charis c0e92057a6 feat(studio): split SQL errors into user and Studio SQL (#53059)
Part of FE-4772.

## Why

Errors from the pg-meta query endpoint didn't say who wrote the SQL, so
a Sentry issue like SUPABASE-APP-KXE couldn't tell a user's query from
one Studio composed. That event came from the assistant's `execute_sql`
tool and was captured by the Vercel AI integration on the server, so it
skipped `reportStudioError` and had no fault tags. The Postgres message
also went to Sentry as-is, and it can contain user SQL and data.

## What

- **`sqlOrigin` is required on `executeSql`.** `'user'` covers SQL the
user or an AI agent wrote, in whole or in part: SQL editor, editor
panel, Explorer, AI `execute_sql` and `run_notebook`, report blocks,
Query Insights EXPLAIN, the policy, function, and cron editors, and
running a stored cron job. Everything else is `'studio'`. The policy,
function, and cron mutation hooks and `useDbQuery` take `sqlOrigin` too.
- **`classifySqlResponseError`** turns a 400 from the query endpoint
into `UserSqlError` (`user`, `user_sql`) or `StudioSqlError` (`studio`,
`studio_sql`). Pattern rules (connection timeouts) and other statuses
keep their classes.
- **`UserSqlError` never reaches Sentry.** Its routing is
`SAMPLE_NEVER`, `alwaysCapture` doesn't override it, and `beforeSend`
drops it.
- **`StudioSqlError` is redacted.** Error classes can set
`sentryMessage`; for `StudioSqlError` it is the SQLSTATE and the query's
`queryName`, a static name that `executeSql` requires for Studio SQL.
The query key is never reported, since its segments can hold IDs,
schemas, and search terms. `applyStudioErrorPolicy` applies it from
`beforeSend` on the client and the server, so it holds however the error
was captured. The server config gets a `beforeSend` for this. Events get
`query_name` and `sql_state` tags and are grouped by both, since every
SQL error shares the `executeSql` stack.
- **A custom `handleError` gets the classified error**, matching its
declared type. The SQL editor, editor panel, and policy editor rethrow
it, so their errors are now `UserSqlError` instead of being reported as
`UnexpectedError`. The error keeps Postgres's `position` for the
editor's line highlight.

The error message, `formattedError`, and `position` stay on the error
for the UI. The query-performance Sentry context no longer carries the
raw error message, which bypassed redaction.

## Known gaps

- Index advisor queries run users' query text from `pg_stat_statements`
but are marked `'studio'`. Redaction keeps the text out of Sentry, but
their failures count as Studio faults.
- 5xx responses for user SQL are still `PlatformError` and keep their
message.
- The self-hosted MCP `executeSql` calls `executeQuery` directly and
isn't covered.

## Testing

- Unit tests for `classifySqlResponseError`, `applyStudioErrorPolicy`,
and the client `beforeSend`.
- Full Studio vitest suite passes except `useReportDateRange.test.ts`
(169 vs 168 hourly buckets across the DST change), which this PR doesn't
touch.
2026-11-09 09:47:35 -05:00
Charis b3dd6612d7 feat(studio): classify errors with fault, reason, and per-tag Sentry sampling (#52730)
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.
2026-11-04 13:19:01 -05:00