mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
## Problem Follow-up to #52730. Resolves part of FE-4772. Fetch failures and aborts were classified as `UnexpectedError`, and HTTP error responses with empty, HTML, or plain-text bodies did too. ## Solution **Network failures and aborts keep their classification.** `fetchHandler` used to rewrite `Failed to fetch` into a plain `Error`, so openapi-fetch network failures were classified as `UnexpectedError`. It now throws `classifyError(err, { endpoint, method })`, giving `NetworkUnreachable` (same user-facing message, original error as `cause`) or `RequestAborted`. The endpoint comes from `schemaPath`, recorded in `onRequest`. Tagged aborts are no longer named `AbortError`, so `isRequestAborted()` checks for both. `state/storage-explorer.tsx` uses it, so cancelling a folder load no longer shows an error toast. **One place reads error responses.** `readResponseError` in `data/error-classification.ts` turns an HTTP error response into a `RawResponseError`. It reads the status, `Retry-After`, the message from a JSON body or a short `text/plain` body (never HTML), plus `requestId`, `endpoint`, and `method`. The openapi-fetch middleware serializes its result into the error body. openapi-fetch builds `error` only from the response body, so that step can't be skipped. `parseResponseErrorBody` now only checks the shape. Empty, HTML, and plain-text error bodies now classify by status. Before, they became `UnexpectedError`. `fetchGet`/`fetchPost`/`fetchHeadWithTimeout` (Studio's own `/api` routes) use the same helper. This also fixes a non-JSON error body making them reject with a `SyntaxError`. **`requestPathname` is replaced by `endpoint`.** The no-retry list and the statement-timeout check in `query-client.ts` match OpenAPI patterns exactly. Critical error messages in `lib/error-reporting.ts` use the pattern, so concrete project refs stay out of them. `path-to-regexp` is no longer used, so it's removed. ## Review instructions 1. From `apps/studio`, run `vitest run data lib components/interfaces/ErrorHandling state`. Expect 4,034 passing tests. 2. Review `readResponseError` in `data/error-classification.ts` and its use in `data/fetchers.ts`. ## Additional context ### Notes - Critical error messages now read `endpoint … w/ message: …`. This regroups those Sentry issues. - A JSON body's `error` string is now accepted as a message for API responses. Before, only the `/api` route path read it. ### Testing - New `data/fetchers-errors.test.ts` covers network failure, abort, empty/HTML/plain-text/JSON error bodies, service derivation, and `/api` route non-JSON bodies. - Unit tests for `readResponseError`, `isRequestAborted`, and the no-retry endpoint set. - `vitest run data lib components/interfaces/ErrorHandling state`: 4,034 pass. ## Checklist Check all before review: - [ ] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
350 lines
12 KiB
TypeScript
350 lines
12 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
classifyError,
|
|
classifyResponseError,
|
|
classifySqlResponseError,
|
|
isRequestAborted,
|
|
NETWORK_UNREACHABLE_MESSAGE,
|
|
parseRateLimitResetSeconds,
|
|
parseResponseErrorBody,
|
|
parseRetryAfterSeconds,
|
|
readResponseError,
|
|
} from './error-classification'
|
|
import {
|
|
ConnectionTimeoutError,
|
|
getServiceForEndpoint,
|
|
isNeverReportedError,
|
|
NetworkUnreachableError,
|
|
PlatformError,
|
|
RequestAbortedError,
|
|
StudioSqlError,
|
|
UnexpectedError,
|
|
UserSqlError,
|
|
} from '@/lib/errors/studio-errors'
|
|
|
|
describe('classifyError', () => {
|
|
it('returns an already-tagged error unchanged', () => {
|
|
const err = new PlatformError({ message: 'x', code: 500 })
|
|
expect(classifyError(err)).toBe(err)
|
|
})
|
|
|
|
it.each([
|
|
['a DOMException', new DOMException('aborted', 'AbortError')],
|
|
['an Error named AbortError', Object.assign(new Error('aborted'), { name: 'AbortError' })],
|
|
])('maps %s to RequestAborted and keeps the cause', (_, thrown) => {
|
|
const err = classifyError(thrown)
|
|
expect(err).toBeInstanceOf(RequestAbortedError)
|
|
expect(err.cause).toBe(thrown)
|
|
})
|
|
|
|
it.each(['Failed to fetch', 'NetworkError when attempting to fetch resource.', 'Load failed'])(
|
|
'maps a fetch-failure TypeError (%s) to NetworkUnreachable',
|
|
(message) => {
|
|
expect(classifyError(new TypeError(message))).toBeInstanceOf(NetworkUnreachableError)
|
|
}
|
|
)
|
|
|
|
it('keeps the user-facing copy and the original error for a fetch failure', () => {
|
|
const thrown = new TypeError('Failed to fetch')
|
|
const err = classifyError(thrown, { endpoint: '/platform/projects/{ref}', method: 'GET' })
|
|
expect(err.message).toBe(NETWORK_UNREACHABLE_MESSAGE)
|
|
expect(err.cause).toBe(thrown)
|
|
expect(err.endpoint).toBe('/platform/projects/{ref}')
|
|
expect(err.method).toBe('GET')
|
|
})
|
|
|
|
it('attributes a fetch failure to the platform while online', () => {
|
|
const err = classifyError(new TypeError('Failed to fetch'))
|
|
expect(err.reason).toBe('network_unreachable')
|
|
expect(err.fault).toBe('platform')
|
|
})
|
|
|
|
it("attributes a fetch failure to the user's network while offline", () => {
|
|
vi.stubGlobal('navigator', { onLine: false })
|
|
const err = classifyError(new TypeError('Failed to fetch'))
|
|
vi.unstubAllGlobals()
|
|
expect(err.reason).toBe('offline')
|
|
expect(err.fault).toBe('user')
|
|
})
|
|
|
|
it('maps a programming-bug TypeError to UnexpectedError', () => {
|
|
const err = classifyError(new TypeError("Cannot read properties of undefined (reading 'x')"))
|
|
expect(err).toBeInstanceOf(UnexpectedError)
|
|
expect(err.fault).toBe('studio')
|
|
})
|
|
|
|
it('maps anything else to UnexpectedError with the cause', () => {
|
|
const thrown = new RangeError('nope')
|
|
const err = classifyError(thrown, { endpoint: '/v1/projects/{ref}', method: 'GET' })
|
|
expect(err).toBeInstanceOf(UnexpectedError)
|
|
expect(err.cause).toBe(thrown)
|
|
expect(err.service).toBe('management-api')
|
|
expect(err.method).toBe('GET')
|
|
})
|
|
})
|
|
|
|
describe('classifyResponseError', () => {
|
|
it('passes endpoint and method through', () => {
|
|
const err = classifyResponseError({
|
|
message: 'x',
|
|
code: 500,
|
|
endpoint: '/platform/projects/{ref}',
|
|
method: 'GET',
|
|
})
|
|
expect(err.endpoint).toBe('/platform/projects/{ref}')
|
|
expect(err.service).toBe('management-api')
|
|
})
|
|
})
|
|
|
|
describe('readResponseError', () => {
|
|
const read = (text: string, headers: Record<string, string> = {}, status = 500) =>
|
|
readResponseError({ status, headers: new Headers(headers), text })
|
|
|
|
it('reads a JSON object body, preferring msg over message over error', () => {
|
|
expect(read(JSON.stringify({ msg: 'a', message: 'b', error: 'c' })).message).toBe('a')
|
|
expect(read(JSON.stringify({ message: 'b', error: 'c' })).message).toBe('b')
|
|
expect(read(JSON.stringify({ error: 'c' })).message).toBe('c')
|
|
})
|
|
|
|
it('reads metadata and formattedError from a JSON body', () => {
|
|
expect(read(JSON.stringify({ message: 'm', metadata: { a: 1 }, formattedError: 'f' }))).toEqual(
|
|
{ message: 'm', code: 500, metadata: { a: 1 }, formattedError: 'f' }
|
|
)
|
|
})
|
|
|
|
it('ignores a JSON array or primitive body', () => {
|
|
expect(read('[1,2]', { 'Content-Type': 'application/json' })).toEqual({ code: 500 })
|
|
expect(read('"hi"')).toEqual({ code: 500 })
|
|
expect(read('42')).toEqual({ code: 500 })
|
|
})
|
|
|
|
it('uses a short text/plain body as the trimmed message, case-insensitively', () => {
|
|
expect(read(' down ', { 'Content-Type': 'Text/Plain; charset=utf-8' }).message).toBe('down')
|
|
})
|
|
|
|
it('ignores a text/plain body longer than the limit', () => {
|
|
expect(read('x'.repeat(501), { 'Content-Type': 'text/plain' }).message).toBeUndefined()
|
|
expect(read('x'.repeat(500), { 'Content-Type': 'text/plain' }).message).toHaveLength(500)
|
|
})
|
|
|
|
it('ignores an HTML body', () => {
|
|
expect(read('<html>bad</html>', { 'Content-Type': 'text/html' }).message).toBeUndefined()
|
|
})
|
|
|
|
it('returns only the status for an empty body', () => {
|
|
expect(read('', {}, 502)).toEqual({ code: 502 })
|
|
})
|
|
|
|
it('reads retryAfter from Retry-After, then X-RateLimit-Reset', () => {
|
|
expect(read('', { 'Retry-After': '7' }).retryAfter).toBe(7)
|
|
expect(read('', { 'X-RateLimit-Reset': '9' }).retryAfter).toBe(9)
|
|
expect(read('', { 'Retry-After': '7', 'X-RateLimit-Reset': '9' }).retryAfter).toBe(7)
|
|
expect(read('').retryAfter).toBeUndefined()
|
|
})
|
|
|
|
it('falls back to X-RateLimit-Reset when Retry-After is malformed', () => {
|
|
expect(read('', { 'Retry-After': '2015-10-21', 'X-RateLimit-Reset': '9' }).retryAfter).toBe(9)
|
|
})
|
|
|
|
it('reads retryAfter from an HTTP-date Retry-After', () => {
|
|
vi.useFakeTimers()
|
|
try {
|
|
vi.setSystemTime(new Date('2015-10-21T07:26:30Z'))
|
|
expect(read('', { 'Retry-After': 'Wed, 21 Oct 2015 07:28:00 GMT' }).retryAfter).toBe(90)
|
|
} finally {
|
|
vi.useRealTimers()
|
|
}
|
|
})
|
|
|
|
it('passes through requestId, endpoint, and method', () => {
|
|
expect(
|
|
readResponseError({
|
|
status: 500,
|
|
headers: new Headers(),
|
|
text: '',
|
|
requestId: 'r1',
|
|
endpoint: '/x',
|
|
method: 'GET',
|
|
})
|
|
).toEqual({ code: 500, requestId: 'r1', endpoint: '/x', method: 'GET' })
|
|
})
|
|
})
|
|
|
|
describe('parseRetryAfterSeconds', () => {
|
|
const now = Date.parse('2015-10-21T07:26:30Z')
|
|
|
|
it('parses delta-seconds', () => {
|
|
expect(parseRetryAfterSeconds('120')).toBe(120)
|
|
expect(parseRetryAfterSeconds(' 7 ')).toBe(7)
|
|
expect(parseRetryAfterSeconds('0')).toBe(0)
|
|
})
|
|
|
|
it('parses an HTTP-date relative to now', () => {
|
|
expect(parseRetryAfterSeconds('Wed, 21 Oct 2015 07:28:00 GMT', now)).toBe(90)
|
|
})
|
|
|
|
it('clamps past HTTP-dates to 0', () => {
|
|
expect(parseRetryAfterSeconds('Wed, 21 Oct 2015 07:00:00 GMT', now)).toBe(0)
|
|
})
|
|
|
|
it.each([null, undefined, '', '-5', '1.5', '7abc', '2015-10-21', 'not a date'])(
|
|
'returns undefined for %j',
|
|
(value) => {
|
|
expect(parseRetryAfterSeconds(value, now)).toBeUndefined()
|
|
}
|
|
)
|
|
})
|
|
|
|
describe('parseRateLimitResetSeconds', () => {
|
|
const now = 1_700_000_000_000
|
|
|
|
it('treats small values as delta-seconds', () => {
|
|
expect(parseRateLimitResetSeconds('9', now)).toBe(9)
|
|
})
|
|
|
|
it('treats large values as epoch seconds', () => {
|
|
expect(parseRateLimitResetSeconds('1700000060', now)).toBe(60)
|
|
expect(parseRateLimitResetSeconds('1699999000', now)).toBe(0)
|
|
})
|
|
|
|
it.each([null, undefined, '', 'abc', '-1', '1.5'])('returns undefined for %j', (value) => {
|
|
expect(parseRateLimitResetSeconds(value, now)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('parseResponseErrorBody', () => {
|
|
it('reads a validated body, accepting msg as a message alias', () => {
|
|
const body = { msg: 'boom', code: 503, requestId: 'r1', retryAfter: 5, endpoint: '/x' }
|
|
expect(parseResponseErrorBody(body)).toMatchObject({
|
|
message: 'boom',
|
|
code: 503,
|
|
requestId: 'r1',
|
|
retryAfter: 5,
|
|
endpoint: '/x',
|
|
})
|
|
})
|
|
|
|
it('keeps the status of a body that has a code but no message', () => {
|
|
expect(parseResponseErrorBody({ code: 500, retryAfter: 5 })).toMatchObject({
|
|
message: undefined,
|
|
code: 500,
|
|
retryAfter: 5,
|
|
})
|
|
})
|
|
|
|
it('returns undefined without a message or a code', () => {
|
|
expect(parseResponseErrorBody({ requestId: 'r1' })).toBeUndefined()
|
|
expect(parseResponseErrorBody(undefined)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('getServiceForEndpoint', () => {
|
|
it.each([
|
|
['/platform/pg-meta/{ref}/query', 'pg-meta'],
|
|
['/v1/projects/{ref}/functions', 'management-api'],
|
|
['/platform/projects/{ref}', 'management-api'],
|
|
['/api/ai/sql', 'unknown'],
|
|
[undefined, 'unknown'],
|
|
])('maps %s to %s', (endpoint, service) => {
|
|
expect(getServiceForEndpoint(endpoint)).toBe(service)
|
|
})
|
|
})
|
|
|
|
describe('classifySqlResponseError', () => {
|
|
const sqlBody = {
|
|
message: 'Failed to run sql query: ERROR: 42702: column reference "schemaname" is ambiguous',
|
|
formattedError: 'ERROR: 42702: column reference "schemaname" is ambiguous\nLINE 32: ...',
|
|
code: 400,
|
|
endpoint: '/platform/pg-meta/{ref}/query',
|
|
}
|
|
|
|
it('classifies a Postgres error from user SQL as UserSqlError, which is never reported', () => {
|
|
const err = classifySqlResponseError(sqlBody, { origin: 'user' })
|
|
expect(err).toBeInstanceOf(UserSqlError)
|
|
expect(err).toMatchObject({ fault: 'user', reason: 'user_sql', message: sqlBody.message })
|
|
expect(isNeverReportedError(err)).toBe(true)
|
|
})
|
|
|
|
it('classifies a Postgres error from Studio SQL as StudioSqlError with the query name', () => {
|
|
const err = classifySqlResponseError(sqlBody, {
|
|
origin: 'studio',
|
|
queryName: 'tables',
|
|
})
|
|
expect(err).toBeInstanceOf(StudioSqlError)
|
|
expect(err).toMatchObject({
|
|
fault: 'studio',
|
|
reason: 'studio_sql',
|
|
queryName: 'tables',
|
|
formattedError: sqlBody.formattedError,
|
|
})
|
|
expect(isNeverReportedError(err)).toBe(false)
|
|
})
|
|
|
|
it.each([
|
|
['a 5xx', { message: 'boom', code: 500 }, PlatformError],
|
|
[
|
|
'a connection timeout',
|
|
{ message: 'Connection terminated due to connection timeout', code: 400 },
|
|
ConnectionTimeoutError,
|
|
],
|
|
])('keeps the usual classification for %s', (_, body, ErrorClass) => {
|
|
expect(classifySqlResponseError(body, { origin: 'user' })).toBeInstanceOf(ErrorClass)
|
|
})
|
|
|
|
it('classifies Postgres errors as SQL errors even when the text matches a pattern rule', () => {
|
|
const body = {
|
|
message:
|
|
'Failed to run sql query: ERROR: P0001: Connection terminated due to connection timeout',
|
|
code: 400,
|
|
}
|
|
expect(classifySqlResponseError(body, { origin: 'user' })).toBeInstanceOf(UserSqlError)
|
|
expect(classifySqlResponseError(body, { origin: 'studio', queryName: 'x' })).toBeInstanceOf(
|
|
StudioSqlError
|
|
)
|
|
})
|
|
|
|
it('keeps a 400 without a SQLSTATE as a SQL error when no pattern matches', () => {
|
|
const body = {
|
|
message: 'Failed to run sql query: ERROR: could not determine which collation to use',
|
|
code: 400,
|
|
}
|
|
expect(classifySqlResponseError(body, { origin: 'user' })).toBeInstanceOf(UserSqlError)
|
|
})
|
|
|
|
it('classifies non-response errors with classifyError', () => {
|
|
const thrown = new TypeError('Failed to fetch')
|
|
expect(classifySqlResponseError(thrown, { origin: 'user' })).toBeInstanceOf(
|
|
NetworkUnreachableError
|
|
)
|
|
})
|
|
|
|
it('reads the SQLSTATE and keeps the Postgres position for the editor', () => {
|
|
const err = classifySqlResponseError({ ...sqlBody, position: '8' }, { origin: 'user' })
|
|
expect(err).toMatchObject({ sqlState: '42702', position: '8' })
|
|
})
|
|
|
|
it('sends Sentry only the SQLSTATE and query name for Studio SQL', () => {
|
|
const err = classifySqlResponseError(sqlBody, { origin: 'studio', queryName: 'tables' })
|
|
expect(err.message).toBe(sqlBody.message)
|
|
expect(err.sentryMessage).toBe('Postgres error 42702 in query tables')
|
|
})
|
|
})
|
|
|
|
describe('isRequestAborted', () => {
|
|
it('accepts raw aborts and RequestAborted errors', () => {
|
|
expect(isRequestAborted(new DOMException('aborted', 'AbortError'))).toBe(true)
|
|
expect(isRequestAborted(new RequestAbortedError({ message: 'aborted' }))).toBe(true)
|
|
expect(isRequestAborted(classifyError(new DOMException('aborted', 'AbortError')))).toBe(true)
|
|
})
|
|
|
|
it.each([
|
|
['a platform error', new PlatformError({ code: 500 })],
|
|
['a plain error', new Error('boom')],
|
|
['undefined', undefined],
|
|
['a string', 'AbortError'],
|
|
])('rejects %s', (_, value) => {
|
|
expect(isRequestAborted(value)).toBe(false)
|
|
})
|
|
})
|