@devries-cameron20 / t3code Mirror · read-only / commits / 23ab75e84a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 21:30 UTC

[codex] Split project command failures (#3459)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 18:19 UTC 23ab75e84ab0680f6e71bf08dfb4b832d4bc4c87
2 files changed +147 -53
apps/server/src/cli/project.test.ts
+9 -4
@@ -2,7 +2,11 @@ import { assert, it } from "@effect/vitest";
2
3 import { EnvironmentInternalError } from "@t3tools/contracts";
4
5 -import { ProjectCommandError } from "./project.ts";
5 +import {
6 + ProjectLiveServerDeclaredResponseError,
7 + ProjectLiveServerRequestError,
8 + projectCommandErrorFromLiveServerRequest,
9 +} from "./project.ts";
10
11 it("maps declared server failures into structural project command errors", () => {
12 const cause = new EnvironmentInternalError({
@@ -11,8 +15,9 @@ it("maps declared server failures into structural project command errors", () =>
15 traceId: "trace-123",
16 });
17
14 - const error = ProjectCommandError.fromLiveServerRequest(cause);
18 + const error = projectCommandErrorFromLiveServerRequest(cause);
19
20 + assert.instanceOf(error, ProjectLiveServerDeclaredResponseError);
21 assert.strictEqual(error.operation, "callLiveServer");
22 assert.strictEqual(error.code, "internal_error");
23 assert.strictEqual(error.traceId, "trace-123");
@@ -23,10 +28,10 @@ it("maps declared server failures into structural project command errors", () =>
28 it("preserves unexpected server failures without deriving the message from them", () => {
29 const cause = new Error("credential abc123 was rejected");
30
26 - const error = ProjectCommandError.fromLiveServerRequest(cause);
31 + const error = projectCommandErrorFromLiveServerRequest(cause);
32
33 + assert.instanceOf(error, ProjectLiveServerRequestError);
34 assert.strictEqual(error.operation, "callLiveServer");
29 - assert.strictEqual(error.detail, "Failed to call the running server.");
35 assert.strictEqual(error.message, "Failed to call the running server.");
36 assert.strictEqual(error.cause, cause);
37 });
apps/server/src/cli/project.ts
+138 -49
@@ -51,62 +51,148 @@ type ProjectCliDispatchCommand = Extract<
51 >;
52
53 const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError);
54 -const ProjectCommandOperation = Schema.Literals([
55 - "generateProjectCommandId",
56 - "callLiveServer",
57 - "validateProjectTitle",
58 - "resolveProjectTarget",
59 - "addProject",
60 -]);
54
62 -export class ProjectCommandError extends Schema.TaggedErrorClass<ProjectCommandError>()(
63 - "ProjectCommandError",
55 +export class ProjectCommandIdGenerationError extends Schema.TaggedErrorClass<ProjectCommandIdGenerationError>()(
56 + "ProjectCommandIdGenerationError",
57 + {
58 + operation: Schema.Literal("generateProjectCommandId"),
59 + cause: Schema.Defect(),
60 + },
61 +) {
62 + override get message(): string {
63 + return "Failed to generate a project command identifier.";
64 + }
65 +}
66 +
67 +export class ProjectLiveServerDeclaredResponseError extends Schema.TaggedErrorClass<ProjectLiveServerDeclaredResponseError>()(
68 + "ProjectLiveServerDeclaredResponseError",
69 + {
70 + operation: Schema.Literal("callLiveServer"),
71 + code: Schema.String,
72 + traceId: Schema.String,
73 + cause: Schema.Defect(),
74 + },
75 +) {
76 + override get message(): string {
77 + return `Server request failed (${this.code}, trace ${this.traceId}).`;
78 + }
79 +}
80 +
81 +export class ProjectLiveServerUndeclaredStatusError extends Schema.TaggedErrorClass<ProjectLiveServerUndeclaredStatusError>()(
82 + "ProjectLiveServerUndeclaredStatusError",
83 + {
84 + operation: Schema.Literal("callLiveServer"),
85 + status: Schema.Int,
86 + cause: Schema.Defect(),
87 + },
88 +) {
89 + override get message(): string {
90 + return `Server request failed with undeclared status ${this.status}.`;
91 + }
92 +}
93 +
94 +export class ProjectLiveServerRequestError extends Schema.TaggedErrorClass<ProjectLiveServerRequestError>()(
95 + "ProjectLiveServerRequestError",
96 + {
97 + operation: Schema.Literal("callLiveServer"),
98 + cause: Schema.Defect(),
99 + },
100 +) {
101 + override get message(): string {
102 + return "Failed to call the running server.";
103 + }
104 +}
105 +
106 +export class ProjectTitleEmptyError extends Schema.TaggedErrorClass<ProjectTitleEmptyError>()(
107 + "ProjectTitleEmptyError",
108 + {
109 + operation: Schema.Literal("validateProjectTitle"),
110 + title: Schema.String,
111 + },
112 +) {
113 + override get message(): string {
114 + return "Project title cannot be empty.";
115 + }
116 +}
117 +
118 +export class ProjectIdentifierEmptyError extends Schema.TaggedErrorClass<ProjectIdentifierEmptyError>()(
119 + "ProjectIdentifierEmptyError",
120 + {
121 + operation: Schema.Literal("resolveProjectTarget"),
122 + identifier: Schema.String,
123 + },
124 +) {
125 + override get message(): string {
126 + return "Project identifier cannot be empty.";
127 + }
128 +}
129 +
130 +export class ProjectNotFoundError extends Schema.TaggedErrorClass<ProjectNotFoundError>()(
131 + "ProjectNotFoundError",
132 {
65 - operation: ProjectCommandOperation,
66 - detail: Schema.String,
67 - code: Schema.optional(Schema.String),
68 - traceId: Schema.optional(Schema.String),
69 - status: Schema.optional(Schema.Number),
133 + operation: Schema.Literal("resolveProjectTarget"),
134 + identifier: Schema.String,
135 + normalizedWorkspaceRoot: Schema.optional(Schema.String),
136 + activeProjectCount: Schema.Number,
137 cause: Schema.optional(Schema.Defect()),
138 },
139 ) {
73 - static fromLiveServerRequest(cause: unknown): ProjectCommandError {
74 - if (isEnvironmentHttpCommonError(cause)) {
75 - return new ProjectCommandError({
76 - operation: "callLiveServer",
77 - detail: `Server request failed (${cause.code}, trace ${cause.traceId}).`,
78 - code: cause.code,
79 - traceId: cause.traceId,
80 - cause,
81 - });
82 - }
83 - if (HttpClientError.isHttpClientError(cause) && cause.response !== undefined) {
84 - return new ProjectCommandError({
85 - operation: "callLiveServer",
86 - detail: `Server request failed with undeclared status ${cause.response.status}.`,
87 - status: cause.response.status,
88 - cause,
89 - });
90 - }
91 - return new ProjectCommandError({
140 + override get message(): string {
141 + return `No active project found for '${this.identifier}'.`;
142 + }
143 +}
144 +
145 +export class ProjectAlreadyExistsError extends Schema.TaggedErrorClass<ProjectAlreadyExistsError>()(
146 + "ProjectAlreadyExistsError",
147 + {
148 + operation: Schema.Literal("addProject"),
149 + projectId: ProjectId,
150 + workspaceRoot: Schema.String,
151 + },
152 +) {
153 + override get message(): string {
154 + return `An active project already exists for '${this.workspaceRoot}'.`;
155 + }
156 +}
157 +
158 +export const ProjectCommandError = Schema.Union([
159 + ProjectCommandIdGenerationError,
160 + ProjectLiveServerDeclaredResponseError,
161 + ProjectLiveServerUndeclaredStatusError,
162 + ProjectLiveServerRequestError,
163 + ProjectTitleEmptyError,
164 + ProjectIdentifierEmptyError,
165 + ProjectNotFoundError,
166 + ProjectAlreadyExistsError,
167 +]);
168 +export type ProjectCommandError = typeof ProjectCommandError.Type;
169 +
170 +export function projectCommandErrorFromLiveServerRequest(cause: unknown): ProjectCommandError {
171 + if (isEnvironmentHttpCommonError(cause)) {
172 + return new ProjectLiveServerDeclaredResponseError({
173 operation: "callLiveServer",
93 - detail: "Failed to call the running server.",
174 + code: cause.code,
175 + traceId: cause.traceId,
176 cause,
177 });
178 }
97 -
98 - override get message(): string {
99 - return this.detail;
179 + if (HttpClientError.isHttpClientError(cause) && cause.response !== undefined) {
180 + return new ProjectLiveServerUndeclaredStatusError({
181 + operation: "callLiveServer",
182 + status: cause.response.status,
183 + cause,
184 + });
185 }
186 +
187 + return new ProjectLiveServerRequestError({ operation: "callLiveServer", cause });
188 }
189
190 const projectCommandUuid = Crypto.Crypto.pipe(
191 Effect.flatMap((crypto) => crypto.randomUUIDv4),
192 Effect.mapError(
193 (cause) =>
107 - new ProjectCommandError({
194 + new ProjectCommandIdGenerationError({
195 operation: "generateProjectCommandId",
109 - detail: "Failed to generate a project command identifier.",
196 cause,
197 }),
198 ),
@@ -158,9 +244,9 @@ const resolveProjectTitle = Effect.fn("resolveProjectTitle")(function* (
244 if (trimmed.length > 0) {
245 return trimmed;
246 }
161 - return yield* new ProjectCommandError({
247 + return yield* new ProjectTitleEmptyError({
248 operation: "validateProjectTitle",
163 - detail: "Project title cannot be empty.",
249 + title: explicitTitle,
250 });
251 }
252
@@ -175,9 +261,9 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* (
261 }) {
262 const trimmedIdentifier = input.identifier.trim();
263 if (trimmedIdentifier.length === 0) {
178 - return yield* new ProjectCommandError({
264 + return yield* new ProjectIdentifierEmptyError({
265 operation: "resolveProjectTarget",
180 - detail: "Project identifier cannot be empty.",
266 + identifier: input.identifier,
267 });
268 }
269
@@ -204,9 +290,11 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* (
290
291 const resolved = exactWorkspaceMatch;
292 if (!resolved) {
207 - return yield* new ProjectCommandError({
293 + return yield* new ProjectNotFoundError({
294 operation: "resolveProjectTarget",
209 - detail: `No active project found for '${trimmedIdentifier}'.`,
295 + identifier: trimmedIdentifier,
296 + activeProjectCount: activeProjects.length,
297 + ...(normalizedWorkspaceRoot === null ? {} : { normalizedWorkspaceRoot }),
298 ...(normalizedWorkspaceRootResult._tag === "Failure"
299 ? { cause: normalizedWorkspaceRootResult.failure }
300 : {}),
@@ -228,7 +316,7 @@ const fetchLiveOrchestrationSnapshot = (origin: string, bearerToken: string) =>
316 });
317 }).pipe(
318 withProjectCliLiveServerTimeout,
231 - Effect.mapError(ProjectCommandError.fromLiveServerRequest),
319 + Effect.mapError(projectCommandErrorFromLiveServerRequest),
320 );
321
322 const dispatchLiveOrchestrationCommand = (
@@ -244,7 +332,7 @@ const dispatchLiveOrchestrationCommand = (
332 } as Parameters<typeof client.orchestration.dispatch>[0]);
333 }).pipe(
334 withProjectCliLiveServerTimeout,
247 - Effect.mapError(ProjectCommandError.fromLiveServerRequest),
335 + Effect.mapError(projectCommandErrorFromLiveServerRequest),
336 );
337
338 const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () {
@@ -376,9 +464,10 @@ const projectAddCommand = Command.make("add", {
464 (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot,
465 );
466 if (existingProject) {
379 - return yield* new ProjectCommandError({
467 + return yield* new ProjectAlreadyExistsError({
468 operation: "addProject",
381 - detail: `An active project already exists for '${workspaceRoot}'.`,
469 + projectId: existingProject.id,
470 + workspaceRoot,
471 });
472 }
473