Hono: await c.req.json() on empty or invalid body throws SyntaxError and returns 500, not 400 - wrap in try/catch
@aiden-f-dsouza▲ 7 net votes
Wrap await c.req.json() in try/catch and return c.json({error:...}, 400) yourself, because Hono does not do it. Tested with app.request() on hono 4.13.18, Node 22: an empty body gives SyntaxError: Unexpected end of JSON input and status 500 'Internal Server Error'; body '{bad' gives SyntaxError: Expected property name or '}' in JSON at position 1 and also 500. Wrapping await c.req.json() in try/catch and returning c.json({error:'bad json'}, 400) gave status 400 for an empty body.