Skip to content

Commit c0471ab

Browse files
0xJannisHugoRCD
andauthored
fix: type error in the error handling section (#236)
Co-authored-by: Hugo Richard <hugo.richard@vercel.com>
1 parent 34d7922 commit c0471ab

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

apps/docs/content/2.frameworks/08.hono.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ app.get('/checkout', (c) => {
126126
Handle errors globally with `app.onError` to return structured JSON responses:
127127

128128
```typescript [src/index.ts]
129+
import type { ContentfulStatusCode } from 'hono/utils/http-status'
130+
129131
app.onError((error, c) => {
130132
c.get('log').error(error)
131133
const parsed = parseError(error)
@@ -137,11 +139,13 @@ app.onError((error, c) => {
137139
fix: parsed.fix,
138140
link: parsed.link,
139141
},
140-
parsed.status,
142+
parsed.status as ContentfulStatusCode,
141143
)
142144
})
143145
```
144146

147+
`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.
148+
145149
The error is captured and logged with both the custom context and structured error fields:
146150

147151
```bash [Terminal output]

examples/vite/src/server.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { serve } from '@hono/node-server'
22
import { Hono } from 'hono'
3+
import type { ContentfulStatusCode } from 'hono/utils/http-status'
34
import { evlog, type EvlogVariables } from 'evlog/hono'
45
import { createError, log, parseError } from 'evlog'
56
import { chargeUser } from './utils/billing'
@@ -47,11 +48,14 @@ app.get('/error', () => {
4748
app.onError((error, c) => {
4849
c.get('log').error(error)
4950
const parsed = parseError(error)
50-
return c.json({
51-
message: parsed.message,
52-
why: parsed.why,
53-
fix: parsed.fix,
54-
}, parsed.status as any)
51+
return c.json(
52+
{
53+
message: parsed.message,
54+
why: parsed.why,
55+
fix: parsed.fix,
56+
},
57+
parsed.status as ContentfulStatusCode,
58+
)
5559
})
5660

5761
serve({ fetch: app.fetch, port: 3000 })

skills/review-logging-patterns/SKILL.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,22 @@ app.get('/api/users', (c) => {
457457

458458
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.
459459

460+
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`).
461+
462+
```typescript
463+
import { createError, parseError } from 'evlog'
464+
import type { ContentfulStatusCode } from 'hono/utils/http-status'
465+
466+
app.onError((error, c) => {
467+
c.get('log').error(error)
468+
const parsed = parseError(error)
469+
return c.json(
470+
{ message: parsed.message, why: parsed.why, fix: parsed.fix, link: parsed.link },
471+
parsed.status as ContentfulStatusCode,
472+
)
473+
})
474+
```
475+
460476
Full pipeline with drain, enrich, and tail sampling:
461477

462478
```typescript

0 commit comments

Comments
 (0)