Template
1
0

feat: add error response support to actions

This commit is contained in:
2025-04-18 22:56:29 +00:00
parent ecb5f80584
commit b0a0494a28
5 changed files with 16 additions and 3 deletions

View File

@@ -1,12 +1,16 @@
import z from "zod";
import { action } from "../../libraries/action.ts";
import { BadRequestError } from "../../mod.ts";
export const addTwoNumbers = action
.make("addTwoNumbers")
.input({ a: z.number(), b: z.number() })
.output({ added: z.number() })
.handle(async ({ a, b }) => {
if (a < 0 || b < 0) {
return new BadRequestError("Invalid input numbers added");
}
return {
added: a + b,
};

View File

@@ -52,4 +52,8 @@ describe("Relay", () => {
it("should successfully run .actions", async () => {
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
});
it("should reject .actions with error", async () => {
assertEquals(await relay.get("/add-two", { a: -1, b: 1 }), 2);
});
});