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,4 +1,5 @@
|
|||
import { createMiddleware } from "hono/factory";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const agentBans = createMiddleware(async (context, next) => {
|
||||
|
|
@ -7,7 +8,7 @@ export const agentBans = createMiddleware(async (context, next) => {
|
|||
|
||||
for (const agent of config.http.banned_user_agents) {
|
||||
if (new RegExp(agent).test(ua)) {
|
||||
return context.json({ error: "Forbidden" }, 403);
|
||||
throw new ApiError(403, "Forbidden");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { createMiddleware } from "hono/factory";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
|
||||
export const boundaryCheck = createMiddleware(async (context, next) => {
|
||||
// Checks that FormData boundary is present
|
||||
|
|
@ -6,11 +7,10 @@ export const boundaryCheck = createMiddleware(async (context, next) => {
|
|||
|
||||
if (contentType?.includes("multipart/form-data")) {
|
||||
if (!contentType.includes("boundary")) {
|
||||
return context.json(
|
||||
{
|
||||
error: "You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
|
||||
},
|
||||
throw new ApiError(
|
||||
400,
|
||||
"Missing FormData boundary",
|
||||
"You are sending a request with a multipart/form-data content type but without a boundary. Please include a boundary in the Content-Type header. For more information, visit https://stackoverflow.com/questions/3508338/what-is-the-boundary-in-multipart-form-data",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { getLogger } from "@logtape/logtape";
|
|||
import type { SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
export const ipBans = createMiddleware(async (context, next) => {
|
||||
|
|
@ -18,7 +19,7 @@ export const ipBans = createMiddleware(async (context, next) => {
|
|||
for (const ip of config.http.banned_ips) {
|
||||
try {
|
||||
if (matches(ip, requestIp?.address)) {
|
||||
return context.json({ error: "Forbidden" }, 403);
|
||||
throw new ApiError(403, "Forbidden");
|
||||
}
|
||||
} catch (e) {
|
||||
const logger = getLogger("server");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue