HTTPException & Error Handlers in Hono

From the Hono cheat sheet · Error Handling · verified Jul 2026

HTTPException & Error Handlers

Handle errors gracefully in your app

typescript
import { HTTPException } from 'hono/http-exception'

// Throw HTTP errors
app.get('/users/:id', async (c) => {
  const user = await db.findUser(c.req.param('id'))
  if (!user) {
    throw new HTTPException(404, { message: 'User not found' })
  }
  return c.json(user)
})

// Global error handler
app.onError((err, c) => {
  if (err instanceof HTTPException) {
    return err.getResponse()
  }
  console.error(err)
  return c.json({ error: 'Internal Server Error' }, 500)
})

// 404 handler
app.notFound((c) => {
  return c.json({ error: 'Not Found', path: c.req.path }, 404)
})
💡 HTTPException creates standard HTTP error responses
⚡ app.onError() catches all unhandled errors
📌 Route-level onError takes precedence over global
🎯 app.notFound() handles 404s
Back to the full Hono cheat sheet