Files
supabase/apps/studio/data/ha-admin/ha-cluster-poolers-query.ts
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## 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.
2026-11-03 11:09:30 -05:00

82 lines
3.3 KiB
TypeScript

import { queryOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { getHaAdmin, parseHaAdminResponse } from './get-ha-admin'
import { haAdminKeys } from './keys'
import { IS_PLATFORM } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
export type HaClusterPoolersVariables = { projectRef?: string }
export type HaClusterPoolersError = StudioError
const shardRefSchema = z
.object({
database: z.string().optional(),
tableGroup: z.string().optional(),
shard: z.string().optional(),
})
.optional()
// Hand-modelled subset of multiadmin's OpenAPI spec. Every field is optional
// because proto3 JSON omits zero values, and enum-shaped fields stay plain
// strings (expected values in comments) so new proto values degrade gracefully
// instead of failing the whole parse.
const multipoolerSchema = z
.object({
id: z.object({ cell: z.string().optional(), name: z.string().optional() }).optional(),
shardRef: shardRefSchema,
shardKey: shardRefSchema,
// 'UNKNOWN' | 'PRIMARY' | 'REPLICA' | 'DRAINED' — deprecated (derived, not
// authoritative), prefer `routingState.role`
type: z.string().optional(),
// 'SERVING' | 'DISABLED' | 'DRAINING'
servingStatus: z.string().optional(),
hostname: z.string().optional(),
// `PoolerLifecycleStatus` (multigres proto/clustermetadata.proto):
// 'STARTING' | 'ACTIVE' | 'STOPPING' | 'SHUTDOWN' | 'QUARANTINED', optionally
// prefixed with 'LIFECYCLE_'. 'LIFECYCLE_UNKNOWN' is the zero value, so it is
// omitted from JSON. There is no dedicated failure member — QUARANTINED is the
// terminal failure state (the pooler gave up recovering, e.g. a failed
// pg_rewind or backup restore, and is kept alive for forensics) and SHUTDOWN is
// "durably down". `getPoolerStatus` maps every member. An unrecognized value
// (a future enum member) falls through to the `servingStatus` check, and since
// SERVING is that enum's zero value it usually renders as Healthy until mapped.
lifecycleStatus: z.object({ status: z.string().optional() }).optional(),
routingState: z
.object({
role: z.string().optional(),
// proto int64s, serialized as strings in JSON
rule: z
.object({ coordinatorTerm: z.string().optional(), leaderSubterm: z.string().optional() })
.optional(),
})
.optional(),
})
.transform(({ shardKey, ...pooler }) => {
pooler.shardRef ??= shardKey
return pooler
})
export const haClusterPoolersResponseSchema = z.object({
poolers: z.array(multipoolerSchema).optional(),
})
export type Multipooler = z.infer<typeof multipoolerSchema>
async function getHaClusterPoolers(
{ projectRef }: HaClusterPoolersVariables,
signal?: AbortSignal
) {
const data = await getHaAdmin(projectRef, 'poolers', signal)
return parseHaAdminResponse(haClusterPoolersResponseSchema, data)
}
export type HaClusterPoolersData = Awaited<ReturnType<typeof getHaClusterPoolers>>
export const haClusterPoolersQueryOptions = ({ projectRef }: HaClusterPoolersVariables) =>
queryOptions({
queryKey: haAdminKeys.poolers(projectRef),
queryFn: ({ signal }) => getHaClusterPoolers({ projectRef }, signal),
enabled: IS_PLATFORM && typeof projectRef !== 'undefined',
})