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.
188 lines
5.3 KiB
TypeScript
188 lines
5.3 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import {
|
|
getCliErrorIssues,
|
|
getCliErrorMessage,
|
|
getStableErrorCode,
|
|
isMissingApiAccessError,
|
|
} from "./error-codes";
|
|
import { BuilderRuntimeError } from "@webstudio-is/project-build/runtime";
|
|
|
|
describe("getStableErrorCode", () => {
|
|
test("reads direct stable error codes", () => {
|
|
expect(
|
|
getStableErrorCode(Object.assign(new Error("Busy"), { code: "BUSY" }))
|
|
).toBe("BUSY");
|
|
});
|
|
|
|
test("reads http-client wrapped error codes", () => {
|
|
expect(
|
|
getStableErrorCode(
|
|
Object.assign(new Error("Not found"), { data: { code: "NOT_FOUND" } })
|
|
)
|
|
).toBe("NOT_FOUND");
|
|
});
|
|
|
|
test("prefers a specific nested Webstudio error code", () => {
|
|
expect(
|
|
getStableErrorCode({
|
|
code: "BAD_REQUEST",
|
|
data: { code: "BAD_REQUEST", webstudioCode: "STALE_INDEX" },
|
|
})
|
|
).toBe("STALE_INDEX");
|
|
});
|
|
|
|
test("reads a stable Webstudio error code from an error cause", () => {
|
|
expect(
|
|
getStableErrorCode(
|
|
Object.assign(new Error("Asset query failed"), {
|
|
cause: { webstudioCode: "CONTENT_DECODING_FAILED" },
|
|
})
|
|
)
|
|
).toBe("CONTENT_DECODING_FAILED");
|
|
});
|
|
|
|
test("looks through generic MCP and internal error wrappers", () => {
|
|
expect(
|
|
getStableErrorCode({
|
|
code: "MCP_TOOL_FAILED",
|
|
cause: {
|
|
code: "INTERNAL_ERROR",
|
|
cause: { webstudioCode: "INVALID_REQUEST" },
|
|
},
|
|
})
|
|
).toBe("INVALID_REQUEST");
|
|
});
|
|
|
|
test("rejects dynamic values as stable error codes", () => {
|
|
expect(getStableErrorCode({ code: "customer-specific-code" })).toBe(
|
|
undefined
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("getCliErrorIssues", () => {
|
|
test("preserves complete asset diagnostic fields", () => {
|
|
expect(
|
|
getCliErrorIssues({
|
|
data: {
|
|
issues: [
|
|
{
|
|
code: "invalid-mdx",
|
|
path: ["posts/broken.mdx"],
|
|
message: "Unexpected end of file",
|
|
constraint: "invalid-mdx",
|
|
severity: "error",
|
|
scope: "query",
|
|
phase: "source",
|
|
assetId: "asset-1",
|
|
line: 4,
|
|
column: 9,
|
|
nodeType: "mdxJsxFlowElement",
|
|
reason: "Closing tag is missing",
|
|
sourceRange: {
|
|
start: { line: 4, column: 1, offset: 30 },
|
|
end: { line: 4, column: 9, offset: 38 },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
})
|
|
).toEqual([
|
|
expect.objectContaining({
|
|
code: "invalid-mdx",
|
|
path: ["posts/broken.mdx"],
|
|
severity: "error",
|
|
scope: "query",
|
|
phase: "source",
|
|
assetId: "asset-1",
|
|
line: 4,
|
|
column: 9,
|
|
nodeType: "mdxJsxFlowElement",
|
|
reason: "Closing tag is missing",
|
|
sourceRange: {
|
|
start: { line: 4, column: 1, offset: 30 },
|
|
end: { line: 4, column: 9, offset: 38 },
|
|
},
|
|
}),
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("getCliErrorMessage", () => {
|
|
test("formats actionable semantic issues for human output", () => {
|
|
const error = new BuilderRuntimeError(
|
|
"INVALID_INPUT",
|
|
"Operation input is invalid.",
|
|
{
|
|
issues: [
|
|
{
|
|
code: "invalid_type",
|
|
path: ["values", "title"],
|
|
message: "Expected a string",
|
|
constraint: "type:string",
|
|
example: "Pricing",
|
|
},
|
|
],
|
|
}
|
|
);
|
|
|
|
expect(getCliErrorMessage(error)).toBe(
|
|
'Operation input is invalid.\nvalues.title: Expected a string (type:string). Example: "Pricing".'
|
|
);
|
|
});
|
|
|
|
test("turns API procedure skew into actionable CLI update guidance", () => {
|
|
const error = {
|
|
data: {
|
|
apiCompatibility: {
|
|
type: "webstudioApiCompatibilityError",
|
|
reason: "apiProcedureNotFound",
|
|
target: "cli",
|
|
message:
|
|
"This version of the Webstudio CLI is incompatible with the current API.",
|
|
action: { type: "updateCli" },
|
|
},
|
|
},
|
|
};
|
|
|
|
expect(getCliErrorMessage(error)).toContain(
|
|
"npm install -g webstudio@latest"
|
|
);
|
|
expect(getCliErrorMessage(error)).toContain("npx webstudio@latest mcp");
|
|
expect(getCliErrorMessage(error, "update-page")).toContain(
|
|
"npx webstudio@latest update-page"
|
|
);
|
|
});
|
|
|
|
test("explains missing Builder API access from structured codes", () => {
|
|
const error = Object.assign(new Error("The credential has been revoked"), {
|
|
data: { code: "UNAUTHORIZED", webstudioCode: "INVALID_AUTH_TOKEN" },
|
|
});
|
|
|
|
expect(isMissingApiAccessError(error)).toBe(true);
|
|
expect(getCliErrorMessage(error)).toBe(
|
|
"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`."
|
|
);
|
|
});
|
|
});
|
|
|
|
test("does not classify authorization failures by message text", () => {
|
|
expect(
|
|
isMissingApiAccessError(
|
|
new Error("Project owner can't be found for token token-1")
|
|
)
|
|
).toBe(false);
|
|
expect(
|
|
isMissingApiAccessError({
|
|
data: { webstudioCode: "API_ACCESS_DISABLED" },
|
|
message: "Any wording",
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
isMissingApiAccessError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: "The credential has been revoked",
|
|
})
|
|
).toBe(false);
|
|
});
|