Last Updated: 3/11/2026
Exception Handling
Hono provides HTTPException for throwing HTTP errors with status codes.
HTTPException
Throw HTTP exceptions:
import { HTTPException } from 'hono/http-exception'
app.post('/auth', async (c) => {
const token = c.req.header('Authorization')
if (!token) {
throw new HTTPException(401, {
message: 'Unauthorized',
})
}
// Continue processing
})Custom Response
throw new HTTPException(401, {
message: 'Custom error message',
res: c.json({ error: 'Unauthorized' }, 401),
})Cause
Include the original error:
try {
await someAsyncOperation()
} catch (err) {
throw new HTTPException(500, {
message: 'Operation failed',
cause: err,
})
}Global Error Handler
Handle all exceptions:
import { HTTPException } from 'hono/http-exception'
app.onError((err, c) => {
if (err instanceof HTTPException) {
return err.getResponse()
}
console.error(err)
return c.json({ error: 'Internal Server Error' }, 500)
})Common Status Codes
// 400 Bad Request
throw new HTTPException(400, { message: 'Invalid input' })
// 401 Unauthorized
throw new HTTPException(401, { message: 'Authentication required' })
// 403 Forbidden
throw new HTTPException(403, { message: 'Access denied' })
// 404 Not Found
throw new HTTPException(404, { message: 'Resource not found' })
// 500 Internal Server Error
throw new HTTPException(500, { message: 'Server error' })