diff --git a/apps/server/src/project/http.test.ts b/apps/server/src/project/http.test.ts new file mode 100644 index 0000000000..a58e6a299b --- /dev/null +++ b/apps/server/src/project/http.test.ts @@ -0,0 +1,52 @@ +import { assert, it } from "@effect/vitest"; +import { ProjectId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; + +import { + ProjectConflictError, + ProjectNotFoundError, + ProjectOperationError, +} from "./ProjectService.ts"; +import { ServerRuntimeStartupError } from "../serverRuntimeStartup.ts"; +import { failProjectMutation } from "./http.ts"; + +const projectId = ProjectId.make("project:http-mutation"); + +it.effect.each([ + new ProjectNotFoundError({ projectId }), + new ProjectConflictError({ + projectId, + workspaceRoot: "/workspace/project", + conflictingProjectId: ProjectId.make("project:http-mutation-conflict"), + }), +])("maps expected project mutation failures to invalid requests", (cause) => + Effect.gen(function* () { + const error = yield* failProjectMutation(cause).pipe(Effect.flip); + + assert.equal(error._tag, "EnvironmentRequestInvalidError"); + assert.equal(error.code, "invalid_request"); + assert.equal(error.reason, "invalid_command"); + }), +); + +it.effect.each([ + new ProjectOperationError({ + operation: "dispatch-project-command", + projectId, + cause: "database unavailable", + }), + new ServerRuntimeStartupError({ + mode: "web", + host: null, + port: 0, + cause: "startup unavailable", + }), +])("keeps operational and startup failures internal", (cause) => + Effect.gen(function* () { + const error = yield* failProjectMutation(cause).pipe(Effect.flip); + + assert.equal(error._tag, "EnvironmentInternalError"); + assert.equal(error.code, "internal_error"); + assert.equal(error.reason, "project_mutation_failed"); + }), +); diff --git a/apps/server/src/project/http.ts b/apps/server/src/project/http.ts index c7c19f087f..cb31c38bfc 100644 --- a/apps/server/src/project/http.ts +++ b/apps/server/src/project/http.ts @@ -9,10 +9,20 @@ import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder"; import { annotateEnvironmentRequest, failEnvironmentInternal, + failEnvironmentInvalidRequest, requireEnvironmentScope, } from "../auth/http.ts"; import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts"; -import { ProjectService } from "./ProjectService.ts"; +import { ProjectService, type ProjectServiceError } from "./ProjectService.ts"; + +export const failProjectMutation = Effect.fn("environment.projects.failMutation")(function* ( + cause: ProjectServiceError | ServerRuntimeStartup.ServerRuntimeStartupError, +) { + if (cause._tag === "ProjectNotFoundError" || cause._tag === "ProjectConflictError") { + return yield* failEnvironmentInvalidRequest("invalid_command"); + } + return yield* failEnvironmentInternal("project_mutation_failed", cause); +}); export const projectHttpApiLayer = HttpApiBuilder.group( EnvironmentHttpApi, @@ -67,11 +77,7 @@ export const projectHttpApiLayer = HttpApiBuilder.group( commandId: mutation.commandId, projectId: mutation.projectId, }); - return yield* startup - .enqueueCommand(operation) - .pipe( - Effect.catch((cause) => failEnvironmentInternal("project_mutation_failed", cause)), - ); + return yield* startup.enqueueCommand(operation).pipe(Effect.catch(failProjectMutation)); }), ); }),