aboutsummaryrefslogtreecommitdiff
path: root/src/config/errorHandler.ts
blob: 43336874184b8b9b943d39427e02c39454f2e470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { HiAnimeError } from "aniwatch";
import type { ErrorHandler, NotFoundHandler } from "hono";

const errResp: { status: number; message: string } = {
  status: 500,
  message: "Internal Server Error",
};

export const errorHandler: ErrorHandler = (err, c) => {
  console.error(err);

  if (err instanceof HiAnimeError) {
    errResp.status = err.status;
    errResp.message = err.message;
  }

  return c.json(errResp, { status: errResp.status });
};

export const notFoundHandler: NotFoundHandler = (c) => {
  errResp.status = 404;
  errResp.message = "Not Found";

  console.error(errResp);
  return c.json(errResp, { status: errResp.status });
};