Files
supabase/apps/studio/lib/error-reporting.test.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

88 lines
3.0 KiB
TypeScript

import * as Sentry from '@sentry/react'
import { describe, expect, it, vi } from 'vitest'
import { captureCriticalError } from './error-reporting'
import { classifyResponseError } from '@/data/error-classification'
vi.mock('@sentry/react', () => ({
captureException: vi.fn(),
withScope: vi.fn((cb: (scope: any) => void) => {
const scope = { setTag: vi.fn() }
cb(scope)
}),
}))
describe('captureCriticalError', () => {
it.each<[string, Parameters<typeof captureCriticalError>[0]]>([
['an object with an empty message', { message: '' }],
['an Error with an empty message', new Error('')],
['an object without a message', { foo: 'bar' } as unknown as { message: string }],
['a whitelisted message', new Error('email must be an email')],
[
'a message containing a whitelisted message',
new Error('User error: A user with this email already exists in the system'),
],
[
'a whitelisted password error',
new Error('Password is known to be weak and easy to guess, please choose a different one'),
],
['a whitelisted TOTP error', new Error('Invalid TOTP code entered')],
['a whitelisted project name error', new Error('name should not contain a . string')],
[
'a 4XX StudioError',
classifyResponseError({ message: 'Not found', code: 404, endpoint: '/api/test' }),
],
[
'a StudioError at the 4XX boundary (499)',
classifyResponseError({
message: 'Client closed request',
code: 499,
endpoint: '/api/test',
}),
],
])('does not capture %s', (_, error) => {
captureCriticalError(error, 'context')
expect(Sentry.captureException).not.toHaveBeenCalled()
})
it.each<[string, Parameters<typeof captureCriticalError>[0], string]>([
['a regular Error', new Error('Database connection failed'), 'Database connection failed'],
[
'a message similar to, but not matching, a whitelisted one',
new Error('email format is invalid'),
'email format is invalid',
],
['an unknown object with a message', { message: 'Custom error object' }, 'Custom error object'],
[
'a StudioError at the 5XX boundary (500)',
classifyResponseError({
message: 'Internal server error',
code: 500,
endpoint: '/api/test',
}),
'endpoint /api/test w/ message: Internal server error',
],
[
'a 5XX StudioError',
classifyResponseError({
message: 'Gateway timeout',
code: 504,
endpoint: '/api/gateway',
}),
'endpoint /api/gateway w/ message: Gateway timeout',
],
[
'a StudioError without code or endpoint',
classifyResponseError({ message: 'Unknown error' }),
'Response Error (no code or endpoint) w/ message: Unknown error',
],
])('captures %s', (_, error, message) => {
captureCriticalError(error, 'context')
expect(Sentry.captureException).toHaveBeenCalledWith(
expect.objectContaining({ message, name: 'CriticalError' })
)
})
})