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.
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
import * as Sentry from '@sentry/nextjs'
|
|
import type { JwtPayload } from '@supabase/supabase-js'
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { apiAuthenticate } from './apiAuthenticate'
|
|
import { apiWrapper } from './apiWrapper'
|
|
import { StudioError } from '@/lib/errors/studio-errors'
|
|
|
|
vi.mock('@/lib/constants', () => ({
|
|
IS_PLATFORM: true,
|
|
API_URL: 'https://api.example.com',
|
|
}))
|
|
|
|
vi.mock('./apiAuthenticate', () => ({
|
|
apiAuthenticate: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@sentry/nextjs', () => ({
|
|
captureException: vi.fn(),
|
|
}))
|
|
|
|
describe('apiWrapper', () => {
|
|
const mockReq = {} as NextApiRequest
|
|
const mockRes = {
|
|
status: vi.fn().mockReturnThis(),
|
|
json: vi.fn().mockReturnThis(),
|
|
} as unknown as NextApiResponse
|
|
const mockHandler = vi.fn()
|
|
|
|
it('should call handler directly when withAuth is false', async () => {
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
|
|
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, undefined)
|
|
expect(apiAuthenticate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should pass JWT claims to handler when withAuth is true', async () => {
|
|
const mockClaims: JwtPayload = {
|
|
iss: 'supabase',
|
|
sub: 'user-123',
|
|
aud: 'authenticated',
|
|
exp: 9999999999,
|
|
iat: 1000000000,
|
|
role: 'authenticated',
|
|
aal: 'aal1',
|
|
session_id: 'session-123',
|
|
}
|
|
vi.mocked(apiAuthenticate).mockResolvedValue(mockClaims)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
|
|
expect(apiAuthenticate).toHaveBeenCalledWith(mockReq, mockRes)
|
|
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, mockClaims)
|
|
})
|
|
|
|
it('should return 401 when authentication fails', async () => {
|
|
const mockError = { error: new StudioError('Invalid token') }
|
|
vi.mocked(apiAuthenticate).mockResolvedValue(mockError)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
|
|
expect(mockRes.status).toHaveBeenCalledWith(401)
|
|
expect(mockHandler).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should report to Sentry and return 500 when the handler throws', async () => {
|
|
const mockError = new Error('boom')
|
|
mockHandler.mockRejectedValue(mockError)
|
|
|
|
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
|
|
expect(Sentry.captureException).toHaveBeenCalledWith(mockError)
|
|
expect(mockRes.status).toHaveBeenCalledWith(500)
|
|
expect(mockRes.json).toHaveBeenCalledWith({ error: mockError })
|
|
})
|
|
})
|