Greenman999
Greenman999
Explore posts from servers
HHono
Created by Greenman999 on 10/23/2024 in #help
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({});
});
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 ^ ?
5 replies