UNPKG

@vigilbase/cli

Version:

Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.

115 lines (114 loc) • 4.43 kB
// GENERATED from packages/api-contract/src/core/errors.ts by scripts/vendor-api-contract.mjs — do not edit import { z } from 'zod'; /** RFC 9457 §3: the media type of every error body on api.vigilbase.com. */ export const PROBLEM_CONTENT_TYPE = 'application/problem+json'; /** * Every `code` a problem body can carry, with its HTTP status. One shared list: * Tools v1 keeps its existing codes (bad_request … service_unavailable, decision 3 * of the contract design), One's key denials keep theirs, and the rest come from * OS's ERROR_CODES so OS can move across later. */ export const ERROR_STATUS = { bad_request: 400, validation_failed: 400, idempotency_key_invalid: 400, unauthorized: 401, invalid_api_key: 401, api_key_revoked: 401, api_key_expired: 401, forbidden: 403, step_up_required: 403, staff_only: 403, read_only_key: 403, not_for_api_keys: 403, insufficient_scope: 403, not_found: 404, method_not_allowed: 405, conflict: 409, idempotency_in_progress: 409, gone: 410, length_required: 411, precondition_failed: 412, payload_too_large: 413, unsupported_media_type: 415, unprocessable_entity: 422, idempotency_key_reused: 422, rate_limit_exceeded: 429, internal_error: 500, upstream_unavailable: 502, service_unavailable: 503, }; export const ERROR_CODES = Object.keys(ERROR_STATUS); export const ErrorCodeSchema = z.enum(ERROR_CODES); /** A field-level validation issue, from zod's issue list. */ export const ProblemFieldError = z.object({ /** JSON Pointer-like dotted path into the part that failed: `body.name`, `query.limit`. */ pointer: z.string(), message: z.string(), }); /** * RFC 9457 problem details (§3.1 members, all present) with the extension * members of the contract (§3.2): `code`, `requestId`, `docs`, field `errors` * and `retryAfterSeconds` where they apply. Extra members are allowed so Tools * v1 can keep its legacy `success`/`error` members. */ export const ProblemDetails = z .looseObject({ type: z.string(), title: z.string(), status: z.number().int().min(400).max(599), detail: z.string(), instance: z.string().optional(), code: ErrorCodeSchema, requestId: z.string(), docs: z.string().optional(), errors: z.array(ProblemFieldError).optional(), retryAfterSeconds: z.number().int().nonnegative().optional(), }) .meta({ id: 'ProblemDetails', description: 'RFC 9457 problem details.' }); const REASON = { 400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 405: 'Method Not Allowed', 409: 'Conflict', 410: 'Gone', 411: 'Length Required', 412: 'Precondition Failed', 413: 'Content Too Large', 415: 'Unsupported Media Type', 422: 'Unprocessable Content', 429: 'Too Many Requests', 500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable', }; export function problemBody(input, config = {}) { const status = ERROR_STATUS[input.code]; const title = config.typeBase ? input.code.split('_').map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join(' ') : (REASON[status] ?? 'Error'); return { ...input.extensions, type: config.typeBase ? `${config.typeBase}/${input.code.replace(/_/g, '-')}` : 'about:blank', title, status, detail: input.detail, ...(input.instance ? { instance: input.instance } : {}), code: input.code, requestId: input.requestId, ...(config.docs ? { docs: config.docs } : {}), ...(input.errors?.length ? { errors: input.errors } : {}), ...(input.retryAfterSeconds !== undefined ? { retryAfterSeconds: input.retryAfterSeconds } : {}), }; } /** Zod issues as problem `errors`, prefixed with the request part that failed. */ export function fieldErrors(part, error) { return error.issues.slice(0, 50).map((issue) => ({ pointer: [part, ...issue.path.map(String)].join('.'), message: issue.message, })); } /** Thrown by implementations to answer with a problem; the dispatcher renders it. */ export class ApiProblem extends Error { code; options; constructor(code, detail, options = {}) { super(detail); this.code = code; this.options = options; this.name = 'ApiProblem'; } get status() { return ERROR_STATUS[this.code]; } }