@rtorcato/api-errors-hono
v0.1.1
Published
Hono middleware for @rtorcato/api-errors (error handler + not-found handler).
Downloads
251
Readme
@rtorcato/api-errors-hono
Hono middleware for @rtorcato/api-errors: error handler + not-found handler.
Install
pnpm add @rtorcato/api-errors @rtorcato/api-errors-hono
pnpm add honohono is a peer dependency (^4).
Usage
import { Hono } from 'hono'
import { NotFoundError } from '@rtorcato/api-errors'
import { errorHandler, notFoundHandler } from '@rtorcato/api-errors-hono'
const app = new Hono()
app.get('/users/:id', (c) => {
if (c.req.param('id') !== '1') throw new NotFoundError('No such user')
return c.json({ id: '1' })
})
app.notFound(notFoundHandler)
app.onError(errorHandler())
export default appResponse shape
For any thrown HttpError (or subclass), the error handler responds with:
{
"error": "NotFoundError",
"code": "not_found",
"message": "No such user"
}Unknown errors map to 500 / internal_server_error. The shape matches @rtorcato/api-errors-express so a frontend consumer sees the same payload regardless of framework.
Options
errorHandler({ includeStack: true })includeStack— appendstackto the response body. Defaults toprocess.env.NODE_ENV === 'development'.
Related
@rtorcato/api-errors— the framework-agnostic error classes@rtorcato/api-errors-express— Express adapter with the same response shape
Source + changelog: https://github.com/rtorcato/api-common/tree/main/packages/api-errors-hono
