Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Float up error code from status page into incident-status endpoint (#43737)

Joshen Lim7d1b38f80466
diff --git a/apps/studio/data/platform/incident-status-query.ts b/apps/studio/data/platform/incident-status-query.ts
index 60cec0fd8d6..3c5ce76b8dd 100644
--- a/apps/studio/data/platform/incident-status-query.ts
+++ b/apps/studio/data/platform/incident-status-query.ts
@@ -2,7 +2,7 @@ import { useQuery } from '@tanstack/react-query'
import type { IncidentInfo } from 'lib/api/incident-status'
import { BASE_PATH, IS_PLATFORM, IS_TEST_ENV } from 'lib/constants'
import { partition } from 'lodash'
-import { UseCustomQueryOptions } from 'types'
+import { ResponseError, UseCustomQueryOptions } from 'types'
import { platformKeys } from './keys'
@@ -21,7 +21,20 @@ export async function getIncidentStatus(
if (!response.ok) {
const errorText = await response.text()
console.error('[getIncidentStatus] Failed:', response.status, errorText)
- throw new Error(`Failed to fetch incident status: ${response.statusText}`)
+
+ let retryAfter: number | undefined
+ const retryAfterHeader = response.headers.get('Retry-After')
+ if (retryAfterHeader !== null) {
+ const parsed = Number(retryAfterHeader)
+ if (Number.isFinite(parsed) && parsed > 0) retryAfter = parsed
+ }
+
+ throw new ResponseError(
+ `Failed to fetch incident status: ${response.statusText}`,
+ response.status,
+ undefined,
+ retryAfter
+ )
}
const data = await response.json()
@@ -37,16 +50,21 @@ export type IncidentStatusError = unknown
export const useIncidentStatusQuery = <TData = IncidentStatusData>(
options: UseCustomQueryOptions<IncidentStatusData, IncidentStatusError, TData> = {}
-) =>
- useQuery<IncidentStatusData, IncidentStatusError, TData>({
+) => {
+ return useQuery<IncidentStatusData, IncidentStatusError, TData>({
queryKey: platformKeys.incidentStatus(),
queryFn: ({ signal }) => getIncidentStatus(signal),
refetchOnWindowFocus: false,
- // exponential backoff retry starting at 4s, 16s, 64s, 256s etc. Hard capped at 5 minutes to prevent excessively
- // long retry delays.
- retryDelay: (attemptIndex) => Math.min(1000 * 4 ** attemptIndex, 1000 * 60 * 5),
- staleTime: 1000 * 60 * 5, // 5 minutes to match API cache
+ // Use longer exponential backoff than the QueryClient default (base 4, cap 5 min vs base 2, cap 30s).
+ // The retryAfter header case is also handled here for consistency with query-client.ts.
+ retryDelay: (attemptIndex, error) => {
+ if (error instanceof ResponseError && error.retryAfter) {
+ return error.retryAfter * 1000
+ }
+ return Math.min(1000 * 4 ** attemptIndex, 1000 * 60 * 5)
+ },
...options,
// Enable in platform mode, or in test environment for E2E testing
enabled: (IS_PLATFORM || IS_TEST_ENV) && (options.enabled ?? true),
})
+}
diff --git a/apps/studio/lib/api/incident-status.ts b/apps/studio/lib/api/incident-status.ts
index f1c3192e1e9..0ea751dcae9 100644
--- a/apps/studio/lib/api/incident-status.ts
+++ b/apps/studio/lib/api/incident-status.ts
@@ -87,9 +87,11 @@ export async function getActiveIncidents(): Promise<IncidentInfo[]> {
const responseText = await response.text()
if (!response.ok) {
+ const retryAfter = response.headers.get('Retry-After') ?? undefined
throw new InternalServerError(`StatusPage API responded with ${response.status}`, {
status: response.status,
body: responseText,
+ ...(retryAfter !== undefined && { retryAfter }),
})
}
diff --git a/apps/studio/pages/api/incident-status.ts b/apps/studio/pages/api/incident-status.ts
index 43b38a9e280..ded8eb02d98 100644
--- a/apps/studio/pages/api/incident-status.ts
+++ b/apps/studio/pages/api/incident-status.ts
@@ -2,19 +2,14 @@ import { IS_PLATFORM } from 'common'
import { NextApiRequest, NextApiResponse } from 'next'
import { InternalServerError } from '@/lib/api/apiHelpers'
-import {
- getActiveIncidents,
- type IncidentCache,
- type IncidentInfo,
-} from '@/lib/api/incident-status'
+import { getActiveIncidents, type IncidentCache } from '@/lib/api/incident-status'
import { createAdminClient } from '@/lib/api/supabase-admin'
/**
- * Cache on browser for 5 minutes
* Cache on CDN for 5 minutes
* Allow serving stale content for 1 minute while revalidating
*/
-const CACHE_CONTROL_SETTINGS = 'public, max-age=300, s-maxage=300, stale-while-revalidate=60'
+const CACHE_CONTROL_SETTINGS = 'public, s-maxage=300, stale-while-revalidate=60'
async function fetchIncidentCache(incidentIds: Array<string>): Promise<Map<string, IncidentCache>> {
const cacheMap = new Map<string, IncidentCache>()
@@ -84,7 +79,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
return res.status(200).json(enrichedIncidents)
} catch (error) {
+ let errorCode = 500
+
if (error instanceof InternalServerError) {
+ if (typeof error.details?.status === 'number') errorCode = error.details.status
+ if (errorCode === 420) errorCode = 429
+ if (errorCode === 429 && typeof error.details?.retryAfter === 'string') {
+ res.setHeader('Retry-After', error.details.retryAfter)
+ }
console.error('Failed to fetch active StatusPage incidents: %O', {
message: error.message,
details: error.details,
@@ -93,7 +95,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
console.error('Unexpected error fetching active StatusPage incidents: %O', error)
}
- return res.status(500).json({ error: 'Unable to fetch incidents at this time' })
+ return res.status(errorCode).json({ error: 'Unable to fetch incidents at this time' })
}
}