mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
Invalid or revoked share links now return an actionable 401 response instead of an unexpected server error. Missing project and workspace records receive distinct responses at their lookup sites. Token, project, and workspace lookups use `.maybeSingle()` and rely on database uniqueness. Missing projects have an explicit `ProjectNotFoundError`; the loader no longer interprets `PGRST116`. The API preserves `UNAUTHORIZED` and `INVALID_AUTH_TOKEN` when authorization fails during context creation, so CLI and MCP clients can offer relinking guidance without inspecting error text. Tokens without API access carry `API_ACCESS_DISABLED`. A repository audit replaces error-message matching with typed errors, stable codes, or structured diagnostics in CLI argument/configuration handling, session conflicts, preview cleanup, import retries, domain unpublishing, asset validation, JSX/expression diagnostics, CSS validation, and publish diagnostics. `AGENTS.md` now explicitly prohibits message-based error classification and recovery. The existing build-permission preflight now advertises structured import-error support. Clients upload assets before importing into older servers that do not advertise it; newer servers keep selective retries using structured missing-file details. The internal template-validation error class remains outside public JSX bindings and input descriptions. Shared Builder `parseError` normalizes tRPC and local validation errors, so UI components only add display context instead of decoding transport payloads. Simplification removes three redundant error classes, catch-and-ignore handlers, an unused formatter, and nested loader error handling. Unpublishing an already unpublished domain succeeds after authorization. CLI configuration failures use the existing shared error handling, and plan parsing catches only JSON parsing failures. Validation: affected suites passed (271 tests across 8 suites in the latest simplification checks), affected package typechecks passed, and lint, package-boundary checks, and `git diff --check` passed. The audit replaced synthetic parser branches and private-method assertions with actual tRPC round trips, PostgREST responses, and public VM behavior. Controlled regressions confirmed that removing serialized validation details and exposing the internal JSX error class both fail the revised tests. Astra reviewed the production changes at medium effort; its compatibility finding and the GitHub JSX-binding comment are fixed, and follow-up review found no remaining substantive issues.
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import {
|
|
getApiCompatibilityMessage,
|
|
getApiErrorCode,
|
|
} from "@webstudio-is/http-client";
|
|
import {
|
|
formatValidationErrorMessage,
|
|
getValidationIssues,
|
|
} from "@webstudio-is/project-build/runtime";
|
|
|
|
const stableErrorCodePattern = /^[A-Z][A-Z0-9_]{0,159}$/;
|
|
|
|
const getErrorMessage = (error: unknown) =>
|
|
typeof error === "object" &&
|
|
error !== null &&
|
|
"message" in error &&
|
|
typeof error.message === "string"
|
|
? error.message
|
|
: error instanceof Error
|
|
? error.message
|
|
: String(error);
|
|
|
|
const genericTransportErrorCodes = new Set([
|
|
"BAD_REQUEST",
|
|
"CONFLICT",
|
|
"FORBIDDEN",
|
|
"INTERNAL_ERROR",
|
|
"INTERNAL_SERVER_ERROR",
|
|
"MCP_RESOURCE_FAILED",
|
|
"MCP_TOOL_FAILED",
|
|
"NOT_FOUND",
|
|
"UNAUTHORIZED",
|
|
"UNKNOWN",
|
|
]);
|
|
|
|
const getStableCode = (value: unknown) =>
|
|
typeof value === "string" && stableErrorCodePattern.test(value)
|
|
? value
|
|
: undefined;
|
|
|
|
export const getStableErrorCode = (error: unknown) => {
|
|
const visited = new Set<unknown>();
|
|
const visit = (value: unknown): string | undefined => {
|
|
if (typeof value !== "object" || value === null || visited.has(value)) {
|
|
return;
|
|
}
|
|
visited.add(value);
|
|
const record = value as Record<string, unknown>;
|
|
const data =
|
|
typeof record.data === "object" && record.data !== null
|
|
? (record.data as Record<string, unknown>)
|
|
: undefined;
|
|
const webstudioCode =
|
|
getStableCode(record.webstudioCode) ?? getStableCode(data?.webstudioCode);
|
|
if (webstudioCode !== undefined) {
|
|
return webstudioCode;
|
|
}
|
|
const directCode = getStableCode(record.code);
|
|
if (
|
|
directCode !== undefined &&
|
|
genericTransportErrorCodes.has(directCode) === false
|
|
) {
|
|
return directCode;
|
|
}
|
|
const nestedCode = visit(record.cause) ?? visit(data?.cause);
|
|
if (nestedCode !== undefined) {
|
|
return nestedCode;
|
|
}
|
|
return directCode ?? getApiErrorCode(value);
|
|
};
|
|
return visit(error);
|
|
};
|
|
|
|
export const isMissingApiAccessError = (error: unknown) => {
|
|
const code = getStableErrorCode(error);
|
|
return code === "INVALID_AUTH_TOKEN" || code === "API_ACCESS_DISABLED";
|
|
};
|
|
|
|
export const getCliErrorSummary = (error: unknown, command = "mcp") => {
|
|
const compatibilityMessage = getApiCompatibilityMessage(error, {
|
|
updateCommand: "npm install -g webstudio@latest",
|
|
runLatestCommand: `npx webstudio@latest ${command}`,
|
|
});
|
|
if (compatibilityMessage !== undefined) {
|
|
return compatibilityMessage;
|
|
}
|
|
const message = getErrorMessage(error);
|
|
if (isMissingApiAccessError(error)) {
|
|
return "This project cannot be accessed through the Builder API with the current share link/token. Enable API access in the share-link settings, then relink the project with `webstudio init --link <share-link> --json`.";
|
|
}
|
|
return message;
|
|
};
|
|
|
|
export const getCliErrorMessage = (error: unknown, command = "mcp") => {
|
|
const message = getCliErrorSummary(error, command);
|
|
const issues = getValidationIssues(error);
|
|
return issues === undefined || issues.length === 0
|
|
? message
|
|
: formatValidationErrorMessage(message, issues);
|
|
};
|
|
|
|
export const getCliErrorIssues = getValidationIssues;
|