mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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.
112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import { onlineManager, QueryClient } from '@tanstack/react-query'
|
|
import { match } from 'path-to-regexp'
|
|
import { useState } from 'react'
|
|
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import { StudioError } from '@/lib/errors/studio-errors'
|
|
|
|
// When running locally we don't need the internet
|
|
// so we can pretend we're online all the time
|
|
if (!IS_PLATFORM) {
|
|
onlineManager.setOnline(true)
|
|
}
|
|
|
|
const SKIP_RETRY_PATHNAME_MATCHERS = [
|
|
'/platform/projects/:ref/run-lints',
|
|
'/platform/organizations/:slug/usage',
|
|
'/platform/pg-meta/:ref/query',
|
|
'/v1/projects/:ref/analytics/endpoints/logs.all',
|
|
].map((pathname) => match(pathname))
|
|
|
|
export const MAX_RETRY_FAILURE_COUNT = 3
|
|
|
|
function isQueryEndpointStatementTimeout(error: unknown) {
|
|
const PG_META_QUERY_PATHNAME_MATCHER = match('/platform/pg-meta/:ref/query')
|
|
return (
|
|
error instanceof StudioError &&
|
|
!!error.requestPathname &&
|
|
!!PG_META_QUERY_PATHNAME_MATCHER(error.requestPathname) &&
|
|
!!error.message?.includes('canceling statement due to statement timeout')
|
|
)
|
|
}
|
|
|
|
let queryClient: QueryClient | undefined
|
|
|
|
export function getQueryClient() {
|
|
const _queryClient =
|
|
queryClient ??
|
|
new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
staleTime: 60 * 1000, // 1 minute
|
|
retry(failureCount, error) {
|
|
// Don't retry on 4xx errors
|
|
if (
|
|
error instanceof StudioError &&
|
|
error.code !== undefined &&
|
|
error.code >= 400 &&
|
|
error.code < 500 &&
|
|
// Still retry on 429s (rate limit)
|
|
error.code !== 429
|
|
) {
|
|
return false
|
|
}
|
|
|
|
// Skip retries for specific pathnames to avoid unnecessary load
|
|
// CRITICAL: We must still retry 429 (rate limit) errors even on these pathnames.
|
|
// Without this exception, queries fail immediately on rate limits, causing the
|
|
// frontend to issue fresh requests (via refetch/user actions), which amplifies
|
|
// the rate limiting problem. By retrying 429s with proper backoff (using the
|
|
// retryAfter header below), we respect rate limits and prevent request storms.
|
|
if (
|
|
error instanceof StudioError &&
|
|
error.requestPathname &&
|
|
SKIP_RETRY_PATHNAME_MATCHERS.some((matchFn) => matchFn(error.requestPathname!)) &&
|
|
error.code !== 429
|
|
) {
|
|
return false
|
|
}
|
|
|
|
if (failureCount < MAX_RETRY_FAILURE_COUNT) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
},
|
|
retryDelay(failureCount, error) {
|
|
if (error instanceof StudioError && error.retryAfter) {
|
|
return error.retryAfter * 1000
|
|
}
|
|
|
|
// react-query default: doubles, starting at 1000ms, with each attempt, but will not exceed 30 seconds
|
|
return Math.min(1000 * 2 ** failureCount, 30000)
|
|
},
|
|
refetchOnWindowFocus(query) {
|
|
// [Joshen] Opting to not refetch a /query request that failed due to a statement
|
|
// timeout, presumably that it'll just run into the same issue. Can however be overriden
|
|
// on a per case basis on each individual query hook
|
|
if (isQueryEndpointStatementTimeout(query.state.error)) return false
|
|
return true
|
|
},
|
|
refetchOnReconnect(query) {
|
|
if (isQueryEndpointStatementTimeout(query.state.error)) return false
|
|
return true
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// For SSG and SSR always create a new queryClient
|
|
if (typeof window === 'undefined') return _queryClient
|
|
// Create the queryClient once in the client
|
|
if (!queryClient) queryClient = _queryClient
|
|
|
|
return queryClient
|
|
}
|
|
|
|
export function useRootQueryClient() {
|
|
const [_queryClient] = useState(() => getQueryClient())
|
|
|
|
return _queryClient
|
|
}
|