mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
111 lines
4.2 KiB
TypeScript
111 lines
4.2 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
ConnectionTimeoutError,
|
|
StudioError,
|
|
UnknownAPIResponseError,
|
|
} from '@/lib/errors/studio-errors'
|
|
|
|
vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() }))
|
|
vi.mock('common', () => ({ IS_PLATFORM: false, getAccessToken: vi.fn() }))
|
|
vi.mock('@/lib/constants', () => ({ API_URL: 'http://localhost' }))
|
|
vi.mock('@/lib/helpers', () => ({ uuidv4: () => 'test-uuid' }))
|
|
|
|
// Import after mocks are set up
|
|
const { handleError } = await import('./fetchers')
|
|
|
|
function throwAndCatch(error: unknown): StudioError {
|
|
try {
|
|
handleError(error)
|
|
} catch (e) {
|
|
return e as StudioError
|
|
}
|
|
throw new Error('handleError did not throw')
|
|
}
|
|
|
|
describe('handleError — error classification', () => {
|
|
// Pattern matching itself (case, whitespace, near-misses) is covered in error-patterns.test.ts.
|
|
it.each([
|
|
['message', { message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT' }],
|
|
['msg', { msg: 'ERROR: CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT' }],
|
|
])('classifies a known pattern from the %s field', (_, raw) => {
|
|
const err = throwAndCatch(raw)
|
|
expect(err).toBeInstanceOf(ConnectionTimeoutError)
|
|
expect(err).toBeInstanceOf(StudioError)
|
|
expect((err as ConnectionTimeoutError).errorType).toBe('connection-timeout')
|
|
})
|
|
|
|
it.each([
|
|
['an unmatched message', { message: 'something went wrong' }],
|
|
['an empty message', { message: '' }],
|
|
['null', null],
|
|
['a non-object', 'raw string error'],
|
|
])('throws UnknownAPIResponseError for %s', (_, raw) => {
|
|
const err = throwAndCatch(raw)
|
|
expect(err).toBeInstanceOf(UnknownAPIResponseError)
|
|
expect(err).toBeInstanceOf(StudioError)
|
|
})
|
|
|
|
describe('field preservation', () => {
|
|
it('preserves all StudioError fields on classified errors', () => {
|
|
const err = throwAndCatch({
|
|
message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT',
|
|
code: 503,
|
|
requestId: 'req-abc',
|
|
retryAfter: 30,
|
|
requestPathname: '/rest/v1/table',
|
|
})
|
|
expect(err.message).toBe('CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT')
|
|
expect(err.code).toBe(503)
|
|
expect(err.requestId).toBe('req-abc')
|
|
expect(err.retryAfter).toBe(30)
|
|
expect(err.requestPathname).toBe('/rest/v1/table')
|
|
})
|
|
|
|
it('preserves all StudioError fields on unclassified errors', () => {
|
|
const err = throwAndCatch({ message: 'some error', code: 500, requestId: 'req-xyz' })
|
|
expect(err.code).toBe(500)
|
|
expect(err.requestId).toBe('req-xyz')
|
|
})
|
|
|
|
it('msg field takes priority over message field for error text', () => {
|
|
const err = throwAndCatch({ msg: 'from msg field', message: 'from message field' })
|
|
expect(err.message).toBe('from msg field')
|
|
})
|
|
|
|
it('preserves formattedError on classified errors', () => {
|
|
const err = throwAndCatch({
|
|
message: 'CONNECTION TERMINATED DUE TO CONNECTION TIMEOUT',
|
|
formattedError: 'ERROR: 08000: CONNECTION TERMINATED\nHINT: retry later',
|
|
})
|
|
expect(err).toBeInstanceOf(ConnectionTimeoutError)
|
|
expect(err.formattedError).toBe('ERROR: 08000: CONNECTION TERMINATED\nHINT: retry later')
|
|
})
|
|
|
|
it('preserves formattedError on unclassified errors (permission denied with HINT)', () => {
|
|
const err = throwAndCatch({
|
|
message: 'permission denied for table users',
|
|
code: 400,
|
|
formattedError:
|
|
'ERROR: 42501: permission denied for table users\n' +
|
|
'HINT: To grant access to anon on a specific table:\n' +
|
|
' GRANT SELECT ON TABLE public.users TO anon;',
|
|
})
|
|
expect(err).toBeInstanceOf(UnknownAPIResponseError)
|
|
expect(err.formattedError).toContain('ERROR:')
|
|
expect(err.formattedError).toContain('HINT:')
|
|
expect(err.formattedError?.split('\n').length).toBeGreaterThan(1)
|
|
})
|
|
|
|
it('leaves formattedError undefined when raw error omits it', () => {
|
|
const err = throwAndCatch({ message: 'some error' })
|
|
expect(err.formattedError).toBeUndefined()
|
|
})
|
|
|
|
it('ignores non-string formattedError values', () => {
|
|
const err = throwAndCatch({ message: 'some error', formattedError: 42 })
|
|
expect(err.formattedError).toBeUndefined()
|
|
})
|
|
})
|
|
})
|