| ... | @@ -51,62 +51,148 @@ type ProjectCliDispatchCommand = Extract< | ... | @@ -51,62 +51,148 @@ type ProjectCliDispatchCommand = Extract< |
| 51 | >; | 51 | >; |
| 52 | | 52 | |
| 53 | const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError); | 53 | const isEnvironmentHttpCommonError = Schema.is(EnvironmentHttpCommonError); |
| 54 | const ProjectCommandOperation = Schema.Literals([ | | |
| 55 | "generateProjectCommandId", | | |
| 56 | "callLiveServer", | | |
| 57 | "validateProjectTitle", | | |
| 58 | "resolveProjectTarget", | | |
| 59 | "addProject", | | |
| 60 | ]); | | |
| 61 | | 54 | |
| 62 | export class ProjectCommandError extends Schema.TaggedErrorClass<ProjectCommandError>()( | 55 | export class ProjectCommandIdGenerationError extends Schema.TaggedErrorClass<ProjectCommandIdGenerationError>()( |
| 63 | "ProjectCommandError", | 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", |
| 64 | { | 132 | { |
| 65 | operation: ProjectCommandOperation, | 133 | operation: Schema.Literal("resolveProjectTarget"), |
| 66 | detail: Schema.String, | 134 | identifier: Schema.String, |
| 67 | code: Schema.optional(Schema.String), | 135 | normalizedWorkspaceRoot: Schema.optional(Schema.String), |
| 68 | traceId: Schema.optional(Schema.String), | 136 | activeProjectCount: Schema.Number, |
| 69 | status: Schema.optional(Schema.Number), | | |
| 70 | cause: Schema.optional(Schema.Defect()), | 137 | cause: Schema.optional(Schema.Defect()), |
| 71 | }, | 138 | }, |
| 72 | ) { | 139 | ) { |
| 73 | static fromLiveServerRequest(cause: unknown): ProjectCommandError { | 140 | override get message(): string { |
| 74 | if (isEnvironmentHttpCommonError(cause)) { | 141 | return `No active project found for '${this.identifier}'.`; |
| 75 | return new ProjectCommandError({ | 142 | } |
| 76 | operation: "callLiveServer", | 143 | } |
| 77 | detail: `Server request failed (${cause.code}, trace ${cause.traceId}).`, | 144 | |
| 78 | code: cause.code, | 145 | export class ProjectAlreadyExistsError extends Schema.TaggedErrorClass<ProjectAlreadyExistsError>()( |
| 79 | traceId: cause.traceId, | 146 | "ProjectAlreadyExistsError", |
| 80 | cause, | 147 | { |
| 81 | }); | 148 | operation: Schema.Literal("addProject"), |
| 82 | } | 149 | projectId: ProjectId, |
| 83 | if (HttpClientError.isHttpClientError(cause) && cause.response !== undefined) { | 150 | workspaceRoot: Schema.String, |
| 84 | return new ProjectCommandError({ | 151 | }, |
| 85 | operation: "callLiveServer", | 152 | ) { |
| 86 | detail: `Server request failed with undeclared status ${cause.response.status}.`, | 153 | override get message(): string { |
| 87 | status: cause.response.status, | 154 | return `An active project already exists for '${this.workspaceRoot}'.`; |
| 88 | cause, | 155 | } |
| 89 | }); | 156 | } |
| 90 | } | 157 | |
| 91 | return new ProjectCommandError({ | 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({ |
| 92 | operation: "callLiveServer", | 173 | operation: "callLiveServer", |
| 93 | detail: "Failed to call the running server.", | 174 | code: cause.code, |
| | 175 | traceId: cause.traceId, |
| 94 | cause, | 176 | cause, |
| 95 | }); | 177 | }); |
| 96 | } | 178 | } |
| 97 | | 179 | if (HttpClientError.isHttpClientError(cause) && cause.response !== undefined) { |
| 98 | override get message(): string { | 180 | return new ProjectLiveServerUndeclaredStatusError({ |
| 99 | return this.detail; | 181 | operation: "callLiveServer", |
| | 182 | status: cause.response.status, |
| | 183 | cause, |
| | 184 | }); |
| 100 | } | 185 | } |
| | 186 | |
| | 187 | return new ProjectLiveServerRequestError({ operation: "callLiveServer", cause }); |
| 101 | } | 188 | } |
| 102 | | 189 | |
| 103 | const projectCommandUuid = Crypto.Crypto.pipe( | 190 | const projectCommandUuid = Crypto.Crypto.pipe( |
| 104 | Effect.flatMap((crypto) => crypto.randomUUIDv4), | 191 | Effect.flatMap((crypto) => crypto.randomUUIDv4), |
| 105 | Effect.mapError( | 192 | Effect.mapError( |
| 106 | (cause) => | 193 | (cause) => |
| 107 | new ProjectCommandError({ | 194 | new ProjectCommandIdGenerationError({ |
| 108 | operation: "generateProjectCommandId", | 195 | operation: "generateProjectCommandId", |
| 109 | detail: "Failed to generate a project command identifier.", | | |
| 110 | cause, | 196 | cause, |
| 111 | }), | 197 | }), |
| 112 | ), | 198 | ), |
| ... | @@ -158,9 +244,9 @@ const resolveProjectTitle = Effect.fn("resolveProjectTitle")(function* ( | ... | @@ -158,9 +244,9 @@ const resolveProjectTitle = Effect.fn("resolveProjectTitle")(function* ( |
| 158 | if (trimmed.length > 0) { | 244 | if (trimmed.length > 0) { |
| 159 | return trimmed; | 245 | return trimmed; |
| 160 | } | 246 | } |
| 161 | return yield* new ProjectCommandError({ | 247 | return yield* new ProjectTitleEmptyError({ |
| 162 | operation: "validateProjectTitle", | 248 | operation: "validateProjectTitle", |
| 163 | detail: "Project title cannot be empty.", | 249 | title: explicitTitle, |
| 164 | }); | 250 | }); |
| 165 | } | 251 | } |
| 166 | | 252 | |
| ... | @@ -175,9 +261,9 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( | ... | @@ -175,9 +261,9 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( |
| 175 | }) { | 261 | }) { |
| 176 | const trimmedIdentifier = input.identifier.trim(); | 262 | const trimmedIdentifier = input.identifier.trim(); |
| 177 | if (trimmedIdentifier.length === 0) { | 263 | if (trimmedIdentifier.length === 0) { |
| 178 | return yield* new ProjectCommandError({ | 264 | return yield* new ProjectIdentifierEmptyError({ |
| 179 | operation: "resolveProjectTarget", | 265 | operation: "resolveProjectTarget", |
| 180 | detail: "Project identifier cannot be empty.", | 266 | identifier: input.identifier, |
| 181 | }); | 267 | }); |
| 182 | } | 268 | } |
| 183 | | 269 | |
| ... | @@ -204,9 +290,11 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( | ... | @@ -204,9 +290,11 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* ( |
| 204 | | 290 | |
| 205 | const resolved = exactWorkspaceMatch; | 291 | const resolved = exactWorkspaceMatch; |
| 206 | if (!resolved) { | 292 | if (!resolved) { |
| 207 | return yield* new ProjectCommandError({ | 293 | return yield* new ProjectNotFoundError({ |
| 208 | operation: "resolveProjectTarget", | 294 | operation: "resolveProjectTarget", |
| 209 | detail: `No active project found for '${trimmedIdentifier}'.`, | 295 | identifier: trimmedIdentifier, |
| | 296 | activeProjectCount: activeProjects.length, |
| | 297 | ...(normalizedWorkspaceRoot === null ? {} : { normalizedWorkspaceRoot }), |
| 210 | ...(normalizedWorkspaceRootResult._tag === "Failure" | 298 | ...(normalizedWorkspaceRootResult._tag === "Failure" |
| 211 | ? { cause: normalizedWorkspaceRootResult.failure } | 299 | ? { cause: normalizedWorkspaceRootResult.failure } |
| 212 | : {}), | 300 | : {}), |
| ... | @@ -228,7 +316,7 @@ const fetchLiveOrchestrationSnapshot = (origin: string, bearerToken: string) => | ... | @@ -228,7 +316,7 @@ const fetchLiveOrchestrationSnapshot = (origin: string, bearerToken: string) => |
| 228 | }); | 316 | }); |
| 229 | }).pipe( | 317 | }).pipe( |
| 230 | withProjectCliLiveServerTimeout, | 318 | withProjectCliLiveServerTimeout, |
| 231 | Effect.mapError(ProjectCommandError.fromLiveServerRequest), | 319 | Effect.mapError(projectCommandErrorFromLiveServerRequest), |
| 232 | ); | 320 | ); |
| 233 | | 321 | |
| 234 | const dispatchLiveOrchestrationCommand = ( | 322 | const dispatchLiveOrchestrationCommand = ( |
| ... | @@ -244,7 +332,7 @@ const dispatchLiveOrchestrationCommand = ( | ... | @@ -244,7 +332,7 @@ const dispatchLiveOrchestrationCommand = ( |
| 244 | } as Parameters<typeof client.orchestration.dispatch>[0]); | 332 | } as Parameters<typeof client.orchestration.dispatch>[0]); |
| 245 | }).pipe( | 333 | }).pipe( |
| 246 | withProjectCliLiveServerTimeout, | 334 | withProjectCliLiveServerTimeout, |
| 247 | Effect.mapError(ProjectCommandError.fromLiveServerRequest), | 335 | Effect.mapError(projectCommandErrorFromLiveServerRequest), |
| 248 | ); | 336 | ); |
| 249 | | 337 | |
| 250 | const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () { | 338 | const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () { |
| ... | @@ -376,9 +464,10 @@ const projectAddCommand = Command.make("add", { | ... | @@ -376,9 +464,10 @@ const projectAddCommand = Command.make("add", { |
| 376 | (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot, | 464 | (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot, |
| 377 | ); | 465 | ); |
| 378 | if (existingProject) { | 466 | if (existingProject) { |
| 379 | return yield* new ProjectCommandError({ | 467 | return yield* new ProjectAlreadyExistsError({ |
| 380 | operation: "addProject", | 468 | operation: "addProject", |
| 381 | detail: `An active project already exists for '${workspaceRoot}'.`, | 469 | projectId: existingProject.id, |
| | 470 | workspaceRoot, |
| 382 | }); | 471 | }); |
| 383 | } | 472 | } |
| 384 | | 473 | |