HonoH
Hono15mo ago
Greenman999

How to prevent Server Error if no req body?

Hi I have this code:

userRouter.patch("/self", async (c) => {
    const reqUserId = c.get("authUser").user?.id;

    if (reqUserId == null) {
        throw new HTTPException(401);
    }

    const { name } = await c.req.json();

    if (!name) {
        throw new HTTPException(400, { message: "Please include a new name in body." });
    }

    return c.json({});
});

If no json is passed in the body i get this error: [ERROR] SyntaxError: Unexpected end of JSON input
Is there anyway to replace this with my own error message?
Like the one in my code ^ ?
Was this page helpful?