mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add proper error type & error handling
This commit is contained in:
parent
c8c6cccf6a
commit
cdceb1a734
14
api/src/base.ts
Normal file
14
api/src/base.ts
Normal file
@ -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");
|
@ -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"],
|
||||
|
@ -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,
|
||||
|
8
api/src/models/error.ts
Normal file
8
api/src/models/error.ts
Normal file
@ -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;
|
@ -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 () => {});
|
||||
|
Loading…
x
Reference in New Issue
Block a user