Skip to content

Commit

Permalink
Merge pull request #3 from bps-kota-bontang/staging
Browse files Browse the repository at this point in the history
Improve error handling based on HTTP status codes
  • Loading branch information
newarifrh authored Aug 25, 2024
2 parents c4ac375 + a02d44f commit 3f3abed
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from "hono/logger";
import { cors } from "hono/cors";
import { csrf } from "hono/csrf";
import { prettyJSON } from "hono/pretty-json";
import { HTTPException } from "hono/http-exception";
import v1 from "@/api/v1";
import { auth } from "@/middlewares/auth";
import { corsOptions } from "@/configs/cors";
Expand Down Expand Up @@ -43,6 +44,19 @@ app.notFound((c) => {
});

app.onError((err, c) => {
if (err instanceof HTTPException) {
const message = err.message;
const status = err.getResponse().status as ResponseInit;

return c.json(
{
message: message,
data: null,
},
status
);
}

return c.json(
{
message: err.message,
Expand Down

0 comments on commit 3f3abed

Please sign in to comment.