mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): ♻️ Throw ApiError instead of returning error JSON
This commit is contained in:
parent
c14621ee06
commit
fbfd237f27
88 changed files with 458 additions and 483 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { apiRoute, applyConfig } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
export const meta = applyConfig({
|
||||
|
|
@ -72,7 +73,7 @@ export default apiRoute((app) =>
|
|||
const buffer = await file.arrayBuffer();
|
||||
|
||||
if (!(await file.exists())) {
|
||||
return context.json({ error: "File not found" }, 404);
|
||||
throw new ApiError(404, "File not found");
|
||||
}
|
||||
|
||||
// Can't directly copy file into Response because this crashes Bun for now
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { apiRoute, applyConfig } from "@/api";
|
|||
import { createRoute } from "@hono/zod-openapi";
|
||||
import type { ContentfulStatusCode, StatusCode } from "hono/utils/http-status";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -57,9 +58,10 @@ export default apiRoute((app) =>
|
|||
|
||||
// Check if URL is valid
|
||||
if (!URL.canParse(id)) {
|
||||
return context.json(
|
||||
{ error: "Invalid URL (it should be encoded as base64url" },
|
||||
throw new ApiError(
|
||||
400,
|
||||
"Invalid URL",
|
||||
"Should be encoded as base64url",
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue