diff --git a/api/src/base.ts b/api/src/base.ts new file mode 100644 index 00000000..d348f900 --- /dev/null +++ b/api/src/base.ts @@ -0,0 +1,14 @@ +import Elysia from "elysia"; +import type { KError } from "./models/error"; + +export const base = new Elysia({ name: "base" }) + .onError(({ code, error }) => { + if (code === "VALIDATION") { + return { + status: error.status, + message: error.message, + details: error, + } as KError; + } + }) + .as("plugin"); diff --git a/api/src/controllers/seed/index.ts b/api/src/controllers/seed/index.ts index 645835fe..467462da 100644 --- a/api/src/controllers/seed/index.ts +++ b/api/src/controllers/seed/index.ts @@ -1,15 +1,15 @@ -import Elysia, { t } from "elysia"; +import Elysia from "elysia"; import { Movie, SeedMovie } from "~/models/movie"; import { seedMovie, SeedMovieResponse } from "./movies"; import { Resource } from "~/models/utils"; import { comment } from "~/utils"; +import { KError } from "~/models/error"; export const seed = new Elysia() .model({ movie: Movie, "seed-movie": SeedMovie, "seed-movie-response": SeedMovieResponse, - error: t.String(), }) .post( "/movies", @@ -25,7 +25,7 @@ export const seed = new Elysia() description: "Existing movie edited/updated.", }, 201: { ...SeedMovieResponse, description: "Created a new movie." }, - 400: "error", + 400: { ...KError, description: "Invalid translation name" }, 409: { ...Resource, description: comment` @@ -33,6 +33,7 @@ export const seed = new Elysia() Change the slug and re-run the request. `, }, + 422: { ...KError, description: "Invalid schema in body." }, }, detail: { tags: ["movies"], diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index 08ac4d35..45b3b8a4 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -171,7 +171,7 @@ export const seedMovie = async ( } return { - status: ret.updated ? "Ok" : "Created", + status: ret.updated ? "OK" : "Created", id: ret.id, slug: ret.slug, videos: retVideos, diff --git a/api/src/models/error.ts b/api/src/models/error.ts new file mode 100644 index 00000000..c795ad12 --- /dev/null +++ b/api/src/models/error.ts @@ -0,0 +1,8 @@ +import { t } from "elysia"; + +export const KError = t.Object({ + status: t.Integer(), + message: t.String(), + details: t.Any(), +}); +export type KError = typeof KError.static; diff --git a/api/tests/seed-movies.test.ts b/api/tests/seed-movies.test.ts index 49e384a4..1173116c 100644 --- a/api/tests/seed-movies.test.ts +++ b/api/tests/seed-movies.test.ts @@ -128,9 +128,35 @@ describe("Movie seeding", () => { expect(body.slug).toBe(existing.slug); }); - test.todo("Missing videos send info", async () => {}); - test.todo("Schema error", async () => {}); - test.todo("Invalid translation name", async () => {}); + it("Missing videos send info", async () => { + const vid = "a0ddf0ce-3258-4452-a670-aff36c76d524"; + const [existing] = await db + .select() + .from(videos) + .where(eq(videos.id, vid)) + .limit(1); + expect(existing).toBeUndefined(); + + const [resp, body] = await createMovie({ + ...dune, + videos: [vid], + }); + + expectStatus(resp, body).toBe(200); + expect(body.videos).toBeArrayOfSize(0); + }); + + it("Schema error (missing fields)", async () => { + const [resp, body] = await createMovie({ + name: "dune", + } as any); + + expectStatus(resp, body).toBe(422); + expect(body.status).toBe(422); + expect(body.message).toBeString(); + expect(body.details).toBeObject(); + // TODO: handle additional fields too + }); test.todo("Create correct video slug (version)", async () => {}); test.todo("Create correct video slug (part)", async () => {}); test.todo("Create correct video slug (rendering)", async () => {});