| ... | ... | @@ -51,62 +51,148 @@ type ProjectCliDispatchCommand = Extract< |
| 51 | 51 | >; |
| 52 | 52 | |
| 53 | 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>()( |
| 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", |
| 64 | 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, |
| 70 | 137 | cause: Schema.optional(Schema.Defect()), |
| 71 | 138 | }, |
| 72 | 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({ |
| 92 | 173 | operation: "callLiveServer", |
| 93 | | detail: "Failed to call the running server.", |
| 174 | code: cause.code, |
| 175 | traceId: cause.traceId, |
| 94 | 176 | cause, |
| 95 | 177 | }); |
| 96 | 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 | }); |
| 100 | 185 | } |
| 186 | |
| 187 | return new ProjectLiveServerRequestError({ operation: "callLiveServer", cause }); |
| 101 | 188 | } |
| 102 | 189 | |
| 103 | 190 | const projectCommandUuid = Crypto.Crypto.pipe( |
| 104 | 191 | Effect.flatMap((crypto) => crypto.randomUUIDv4), |
| 105 | 192 | Effect.mapError( |
| 106 | 193 | (cause) => |
| 107 | | new ProjectCommandError({ |
| 194 | new ProjectCommandIdGenerationError({ |
| 108 | 195 | operation: "generateProjectCommandId", |
| 109 | | detail: "Failed to generate a project command identifier.", |
| 110 | 196 | cause, |
| 111 | 197 | }), |
| 112 | 198 | ), |
| ... | ... | @@ -158,9 +244,9 @@ const resolveProjectTitle = Effect.fn("resolveProjectTitle")(function* ( |
| 158 | 244 | if (trimmed.length > 0) { |
| 159 | 245 | return trimmed; |
| 160 | 246 | } |
| 161 | | return yield* new ProjectCommandError({ |
| 247 | return yield* new ProjectTitleEmptyError({ |
| 162 | 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 | 261 | }) { |
| 176 | 262 | const trimmedIdentifier = input.identifier.trim(); |
| 177 | 263 | if (trimmedIdentifier.length === 0) { |
| 178 | | return yield* new ProjectCommandError({ |
| 264 | return yield* new ProjectIdentifierEmptyError({ |
| 179 | 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 | 290 | |
| 205 | 291 | const resolved = exactWorkspaceMatch; |
| 206 | 292 | if (!resolved) { |
| 207 | | return yield* new ProjectCommandError({ |
| 293 | return yield* new ProjectNotFoundError({ |
| 208 | 294 | operation: "resolveProjectTarget", |
| 209 | | detail: `No active project found for '${trimmedIdentifier}'.`, |
| 295 | identifier: trimmedIdentifier, |
| 296 | activeProjectCount: activeProjects.length, |
| 297 | ...(normalizedWorkspaceRoot === null ? {} : { normalizedWorkspaceRoot }), |
| 210 | 298 | ...(normalizedWorkspaceRootResult._tag === "Failure" |
| 211 | 299 | ? { cause: normalizedWorkspaceRootResult.failure } |
| 212 | 300 | : {}), |
| ... | ... | @@ -228,7 +316,7 @@ const fetchLiveOrchestrationSnapshot = (origin: string, bearerToken: string) => |
| 228 | 316 | }); |
| 229 | 317 | }).pipe( |
| 230 | 318 | withProjectCliLiveServerTimeout, |
| 231 | | Effect.mapError(ProjectCommandError.fromLiveServerRequest), |
| 319 | Effect.mapError(projectCommandErrorFromLiveServerRequest), |
| 232 | 320 | ); |
| 233 | 321 | |
| 234 | 322 | const dispatchLiveOrchestrationCommand = ( |
| ... | ... | @@ -244,7 +332,7 @@ const dispatchLiveOrchestrationCommand = ( |
| 244 | 332 | } as Parameters<typeof client.orchestration.dispatch>[0]); |
| 245 | 333 | }).pipe( |
| 246 | 334 | withProjectCliLiveServerTimeout, |
| 247 | | Effect.mapError(ProjectCommandError.fromLiveServerRequest), |
| 335 | Effect.mapError(projectCommandErrorFromLiveServerRequest), |
| 248 | 336 | ); |
| 249 | 337 | |
| 250 | 338 | const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () { |
| ... | ... | @@ -376,9 +464,10 @@ const projectAddCommand = Command.make("add", { |
| 376 | 464 | (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot, |
| 377 | 465 | ); |
| 378 | 466 | if (existingProject) { |
| 379 | | return yield* new ProjectCommandError({ |
| 467 | return yield* new ProjectAlreadyExistsError({ |
| 380 | 468 | operation: "addProject", |
| 381 | | detail: `An active project already exists for '${workspaceRoot}'.`, |
| 469 | projectId: existingProject.id, |
| 470 | workspaceRoot, |
| 382 | 471 | }); |
| 383 | 472 | } |
| 384 | 473 | |