6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
export interface ApiClientErrorOptions {
|
|
status: number
|
|
message: string
|
|
body: unknown
|
|
rawBody?: string
|
|
code?: string
|
|
}
|
|
|
|
export class ApiClientError extends Error {
|
|
readonly status: number
|
|
readonly body: unknown
|
|
readonly rawBody?: string
|
|
readonly code?: string
|
|
|
|
constructor(options: ApiClientErrorOptions) {
|
|
super(options.message)
|
|
this.name = 'ApiClientError'
|
|
this.status = options.status
|
|
this.body = options.body
|
|
this.rawBody = options.rawBody
|
|
this.code = options.code
|
|
}
|
|
}
|
|
|
|
export function isApiClientError(error: unknown): error is ApiClientError {
|
|
return error instanceof ApiClientError
|
|
}
|
|
|
|
export interface ValidationIssue {
|
|
/** Path of the failing field, e.g. ['updates', 'name']. */
|
|
path: ReadonlyArray<string | number>
|
|
/** Human-readable message — uses the schema's custom error string when set. */
|
|
message: string
|
|
}
|
|
|
|
interface UnknownIssue {
|
|
path?: unknown
|
|
message?: unknown
|
|
}
|
|
|
|
function normalizeIssue(raw: unknown): ValidationIssue | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const { path, message } = raw as UnknownIssue
|
|
if (typeof message !== 'string' || message.length === 0) return null
|
|
if (!Array.isArray(path)) return null
|
|
const cleanPath = path.filter(
|
|
(segment): segment is string | number =>
|
|
typeof segment === 'string' || typeof segment === 'number'
|
|
)
|
|
return { path: cleanPath, message }
|
|
}
|
|
|
|
/**
|
|
* Pull a list of validation issues out of an unknown error. Recognises both
|
|
* shapes the boundary produces:
|
|
*
|
|
* - Client-side contract validation: `requestJson` calls `schema.parse(input)`
|
|
* before fetch; failure throws a raw `ZodError` whose `.issues` is the array.
|
|
* - Server-side contract validation: route returns `{ error, details: [...] }`,
|
|
* which `requestJson` re-throws as `ApiClientError` carrying the body.
|
|
*
|
|
* Returns an empty array when the error isn't a recognised validation shape so
|
|
* callers can fall back to toast/log paths.
|
|
*/
|
|
export function extractValidationIssues(error: unknown): ValidationIssue[] {
|
|
if (!error || typeof error !== 'object') return []
|
|
|
|
if (isApiClientError(error)) {
|
|
const body = error.body
|
|
if (body && typeof body === 'object') {
|
|
const details = (body as { details?: unknown }).details
|
|
if (Array.isArray(details)) {
|
|
return details.map(normalizeIssue).filter((i): i is ValidationIssue => i !== null)
|
|
}
|
|
}
|
|
return []
|
|
}
|
|
|
|
const issues = (error as { issues?: unknown }).issues
|
|
if (Array.isArray(issues)) {
|
|
return issues.map(normalizeIssue).filter((i): i is ValidationIssue => i !== null)
|
|
}
|
|
return []
|
|
}
|
|
|
|
/**
|
|
* Match a single issue by suffix path. `pathSuffix` lets callers ignore the
|
|
* outer body wrapper — `findValidationIssue(err, ['name'])` matches both
|
|
* `path: ['name']` and `path: ['updates', 'name']`.
|
|
*/
|
|
export function findValidationIssue(
|
|
error: unknown,
|
|
pathSuffix: ReadonlyArray<string | number>
|
|
): ValidationIssue | null {
|
|
const issues = extractValidationIssues(error)
|
|
for (const issue of issues) {
|
|
if (issue.path.length < pathSuffix.length) continue
|
|
const tail = issue.path.slice(issue.path.length - pathSuffix.length)
|
|
if (tail.every((segment, i) => segment === pathSuffix[i])) return issue
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** True when the error is a recognised validation failure (client or server). */
|
|
export function isValidationError(error: unknown): boolean {
|
|
return extractValidationIssues(error).length > 0
|
|
}
|