fix(server): preserve project mutation client errors
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
55ff7a676f
commit
729304a428
2 files changed
+64
-6
No files matched your search
@@ -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");
|
||||
}),
|
||||
);
|
||||
@@ -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));
|
||||
}),
|
||||
);
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user