Files
supabase/apps/studio/data/logs/unified-log-attributes-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

83 lines
2.8 KiB
TypeScript

import { queryOptions } from '@tanstack/react-query'
import { z } from 'zod'
import { executeAnalyticsSql, type ExecuteAnalyticsSqlVariables } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { UNIFIED_LOGS_QUERY_OPTIONS } from './unified-logs-infinite-query'
import { getLogAttributesQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { handleError } from '@/data/fetchers'
import { IS_PLATFORM } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
const LOG_WINDOW_MS = 60 * 1000
const logAttributesRowSchema = z.object({
source: z.string(),
log_attributes: z.record(z.string(), z.unknown()).nullish(),
})
export type UnifiedLogAttributesVariables = {
projectRef?: string
logId?: string
source?: string
logTimestampMs?: number | null
}
export type UnifiedLogAttributesError = StudioError
export const getLogTimeWindow = (timestampMs: number, windowMs: number) => ({
iso_timestamp_start: new Date(timestampMs - windowMs).toISOString(),
iso_timestamp_end: new Date(timestampMs + windowMs).toISOString(),
})
export async function runOtelLogsSql(variables: Omit<ExecuteAnalyticsSqlVariables, 'endpoint'>) {
const data = await executeAnalyticsSql({
...variables,
endpoint: '/platform/projects/{ref}/analytics/endpoints/logs.all.otel',
})
if (data?.error) handleError(data.error)
return data?.result ?? []
}
async function getUnifiedLogAttributes(
{ projectRef, logId, source, logTimestampMs }: UnifiedLogAttributesVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
if (!logId) throw new Error('logId is required')
if (!source) throw new Error('source is required')
if (typeof logTimestampMs !== 'number' || !Number.isFinite(logTimestampMs)) {
throw new Error('logTimestampMs is required')
}
const result = await runOtelLogsSql({
projectRef,
sql: getLogAttributesQuery({ logId, source }),
...getLogTimeWindow(logTimestampMs, LOG_WINDOW_MS),
signal,
})
return z.array(logAttributesRowSchema).parse(result)[0] ?? null
}
export type UnifiedLogAttributesData = Awaited<ReturnType<typeof getUnifiedLogAttributes>>
export const unifiedLogAttributesQueryOptions = ({
projectRef,
logId,
source,
logTimestampMs,
}: UnifiedLogAttributesVariables) =>
queryOptions<UnifiedLogAttributesData, UnifiedLogAttributesError>({
queryKey: logsKeys.logAttributes(projectRef, logId, source, logTimestampMs),
queryFn: ({ signal }) =>
getUnifiedLogAttributes({ projectRef, logId, source, logTimestampMs }, signal),
enabled:
IS_PLATFORM &&
typeof projectRef !== 'undefined' &&
typeof logId !== 'undefined' &&
typeof source !== 'undefined' &&
typeof logTimestampMs === 'number' &&
Number.isFinite(logTimestampMs),
...UNIFIED_LOGS_QUERY_OPTIONS,
})