Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/docs/content/2.frameworks/08.hono.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ app.get('/checkout', (c) => {
Handle errors globally with `app.onError` to return structured JSON responses:

```typescript [src/index.ts]
import type { ContentfulStatusCode } from 'hono/utils/http-status'

app.onError((error, c) => {
c.get('log').error(error)
const parsed = parseError(error)
Expand All @@ -137,11 +139,13 @@ app.onError((error, c) => {
fix: parsed.fix,
link: parsed.link,
},
parsed.status,
parsed.status as ContentfulStatusCode,
)
})
```

`parseError()` types `status` as a `number`, while Hono’s `c.json()` second argument expects `ContentfulStatusCode`. The cast matches what you already return at runtime and satisfies TypeScript.

The error is captured and logged with both the custom context and structured error fields:

```bash [Terminal output]
Expand Down
14 changes: 9 additions & 5 deletions examples/vite/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import type { ContentfulStatusCode } from 'hono/utils/http-status'
import { evlog, type EvlogVariables } from 'evlog/hono'
import { createError, log, parseError } from 'evlog'
import { chargeUser } from './utils/billing'
Expand Down Expand Up @@ -47,11 +48,14 @@ app.get('/error', () => {
app.onError((error, c) => {
c.get('log').error(error)
const parsed = parseError(error)
return c.json({
message: parsed.message,
why: parsed.why,
fix: parsed.fix,
}, parsed.status as any)
return c.json(
{
message: parsed.message,
why: parsed.why,
fix: parsed.fix,
},
parsed.status as ContentfulStatusCode,
)
})

serve({ fetch: app.fetch, port: 3000 })
Expand Down
16 changes: 16 additions & 0 deletions skills/review-logging-patterns/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,22 @@ app.get('/api/users', (c) => {

Access the logger via `c.get('log')` in handlers. No `useLogger()` — use `c.get('log')` and pass it down explicitly, or use Express/Fastify/Elysia if you need `useLogger()` across async boundaries.

Structured errors: throw `createError()`, then in `app.onError` use `parseError()` and pass `parsed.status as ContentfulStatusCode` to `c.json()` (Hono types the status argument as `ContentfulStatusCode`, not `number`).

```typescript
import { createError, parseError } from 'evlog'
import type { ContentfulStatusCode } from 'hono/utils/http-status'

app.onError((error, c) => {
c.get('log').error(error)
const parsed = parseError(error)
return c.json(
{ message: parsed.message, why: parsed.why, fix: parsed.fix, link: parsed.link },
parsed.status as ContentfulStatusCode,
)
})
```

Full pipeline with drain, enrich, and tail sampling:

```typescript
Expand Down