Files
supabase/apps/studio/lib/errors/report-studio-error.ts
T
Alaister YoungandAlaister Young bbb0af65cb [FE-3106] (part 14) – refactor(studio): drop the dual-runtime shims and aliases (#53223)
Drops the remaining Next-era shims and aliases that only existed for the
dual runtime (FE-3106). Part 14 of the stack.

**Depends on:** #53222

**Changed:**
- ~45 files – import Sentry from `@sentry/react` directly instead of the
aliased `@sentry/nextjs`
- `hooks/ui/usePreventNavigationOnUnsavedChanges.ts` – is now the
TanStack `useBlocker` implementation (previously swapped in by a Vite
alias)
- `components/interfaces/App/RouteValidationWrapper.tsx` – `next/router`
→ `useNavigate` + the leaf route's `fullPath` (`/project/$ref/sql/$id`)
for the exempt-route and last-visited checks
- `hooks/misc/withAuth.tsx` – no `getLayout` forwarding
- `TemplateEditor.test.tsx` – renders inside a router for the
unsaved-changes blocker

**Removed:**
- `compat/sentry-nextjs.ts` and its Vite alias, the
`usePreventNavigationOnUnsavedChanges` alias and Next hook,
`types/next.ts` (`NextPageWithLayout`), the `@sentry/nextjs` dependency

The `compat/next/*` shims for `link`, `router`, `head`, `image`,
`navigation`, `dynamic` and `script` stay: about 300 components outside
`routes/**` still import them.

Addresses
[FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages).

## To test

- Trigger an error (e.g. a failing query) – it reaches Sentry as before
- Edit an email template or edge function and navigate away – the
unsaved-changes prompt appears
- Open an org you don't belong to – toast and redirect home; open a SQL
snippet, leave and come back – last snippet is restored

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-17 17:49:20 +09:00

71 lines
2.7 KiB
TypeScript

import * as Sentry from '@sentry/react'
import { getErrorRouting, SAMPLE_ALWAYS, SAMPLE_NEVER } from './error-routing'
import { classifyError } from '@/data/error-classification'
import { isNeverReportedError, type StudioError } from '@/lib/errors/studio-errors'
export type StudioSentryContext = {
tags?: Record<string, string | number | boolean>
extra?: Record<string, unknown>
contexts?: Record<string, Record<string, unknown>>
}
/**
* Sends the error to Sentry when its routing sample rate is above zero. The rate is attached as
* the `sample_rate` tag; `beforeSend` applies it. Records the event id on the error.
* `force` is for callers that explicitly asked for capture (`alwaysCapture`) and overrides routing,
* except for errors that are never reported (`isNeverReportedError`).
*/
export function reportStudioError(
err: StudioError,
context?: StudioSentryContext,
{ force = false }: { force?: boolean } = {}
) {
if (isNeverReportedError(err)) return
const sampleRate = force ? SAMPLE_ALWAYS : getErrorRouting(err).sentrySampleRate
if (sampleRate <= SAMPLE_NEVER) return
if (err.errorEventId) return
const eventId = Sentry.captureException(err, {
...context,
tags: {
...context?.tags,
error_tag: err._tag,
sample_rate: sampleRate,
fault: err.fault,
reason: err.reason,
classified_by: err.classifiedBy,
service: err.service,
...(err.endpoint && { endpoint: err.endpoint }),
...(err.queryName && { query_name: err.queryName }),
...(err.sqlState && { sql_state: err.sqlState }),
},
extra: { ...context?.extra, requestId: err.requestId, status: err.code },
})
err.errorEventId = eventId
}
type DeferredReportOptions = { context?: StudioSentryContext; force?: boolean }
// Kept off the error so its fields describe only the error, not how the caller wants it reported.
const deferredReportOptions = new WeakMap<StudioError, DeferredReportOptions>()
/**
* Remembers the reporting options a caller passed to `handleError`, keyed by the error instance, so
* the query cache can report with them after the last retry (see `reportQueryError`).
*/
export function deferStudioErrorReport(err: StudioError, options: DeferredReportOptions) {
deferredReportOptions.set(err, options)
}
/**
* Reports an error that exhausted its React Query retries. Called from the `QueryCache` and
* `MutationCache` `onError`. Errors thrown by `handleError` are already classified; anything else
* (e.g. a query function that throws its own `Error`) is classified here.
*/
export function reportQueryError(error: unknown) {
const classified = classifyError(error)
const options = deferredReportOptions.get(classified)
reportStudioError(classified, options?.context, { force: options?.force })
}