Files
webstudio/apps/builder/app/services/api-errors.server.ts
Oleg Isonen 2808f06808 feat: Improve MCP validation errors with actionable guidance (#5881)
## What changed

- Return structured validation issues with stable codes, complete field
paths, constraints, and minimal correction examples.
- Preserve validation details through the Builder API, CLI, MCP, local
project sessions, and tRPC errors.
- Explain invalid expressions, JSX fragments, page selectors, component
inputs, asset updates, and project settings in terms an agent can act
on.
- Remove stack frames and credentials from parser details before
exposing them.
- Keep concise human-readable errors while making the complete
structured issues available to agents.

## Why

Agents previously received vague errors without enough information to
repair a request. They could repeat the same invalid call or require
manual intervention.

## User impact

CLI and MCP agents can now identify the exact invalid field, understand
the constraint, see a valid example, and retry with a corrected request.

## Validation

- Focused runtime, MCP, CLI, API, and tRPC validation tests.
- Structured error propagation tests across local and server execution.
- Typechecks and generated contract verification.
2026-07-15 11:33:36 +02:00

19 lines
474 B
TypeScript

import { TRPCError } from "@trpc/server";
type ApiErrorOptions = {
webstudioCode?: string;
issues?: unknown;
};
export const throwApiError = (
code: "BAD_REQUEST" | "NOT_FOUND" | "CONFLICT",
message: string,
options: ApiErrorOptions = {}
): never => {
const cause =
options.webstudioCode === undefined && options.issues === undefined
? undefined
: Object.assign(new Error(message), options);
throw new TRPCError({ code, message, cause });
};