Mickaël
Mickaël
HHono
Created by Davis on 6/12/2024 in #help
How to set up and use a structured error format?
Another one is create some classes that extends HTTPException, if you want type safety and more complex error objects.
3 replies
HHono
Created by Davis on 6/12/2024 in #help
How to set up and use a structured error format?
Hi, a solution for this problem is to catch all errors and format them in one place, like this :
const app = new Hono()
.onError((error, c) => {
if (error.status === 404) {
return c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'NotFound',
message: error.message ?? 'Entity not found',
},
},
404,
)
: c.html(<NotFoundPage />, 404)
}
}
...the same for all status code you like to support...
return c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'ServerError',
message: 'An unknown error occurred',
},
},
500,
)
: c.html(<ServerErrorPage />, 500)
})
.notFound((c) =>
c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'ApiNotFound',
message: 'Api not found',
},
},
404,
)
: c.html(<NotFoundPage />, 404),
)
const app = new Hono()
.onError((error, c) => {
if (error.status === 404) {
return c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'NotFound',
message: error.message ?? 'Entity not found',
},
},
404,
)
: c.html(<NotFoundPage />, 404)
}
}
...the same for all status code you like to support...
return c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'ServerError',
message: 'An unknown error occurred',
},
},
500,
)
: c.html(<ServerErrorPage />, 500)
})
.notFound((c) =>
c.req.path.startsWith('/api/')
? c.json(
{
error: {
code: 'ApiNotFound',
message: 'Api not found',
},
},
404,
)
: c.html(<NotFoundPage />, 404),
)
So when you throw an error like this :
import { HTTPException } from 'hono/http-exception'

throw new HTTPException(404, { message: `Product ${id} not found` })
import { HTTPException } from 'hono/http-exception'

throw new HTTPException(404, { message: `Product ${id} not found` })
The error is formatted to your app standard error format.
3 replies
HHono
Created by Arjun on 6/10/2024 in #help
Can someone explain why types aren't inferred
Do you think this kind of functionality would be added via Typescript 5.5 - Inferred Type Predicates ?
11 replies
HHono
Created by Arjun on 6/10/2024 in #help
Can someone explain why types aren't inferred
Yes if it's a Typescript limitation then you have to live with it ^^
11 replies
HHono
Created by Arjun on 6/10/2024 in #help
Can someone explain why types aren't inferred
Yes, and it's a problem in the sense that the documentation is full of examples that don't use chaining. I personally chose to chain all the methods on my project, but I have to admit that this has an impact on readability. Here's a related issue: https://github.com/honojs/hono/issues/2120#issuecomment-1917847801
11 replies
HHono
Created by Arjun on 6/10/2024 in #help
Can someone explain why types aren't inferred
The difference is that on the left, he doesn't chain methods In my case too, without chaining, type inference doesn't work
11 replies
HHono
Created by chip on 5/30/2024 in #help
How to debug Hono/HonoX in Visual Studio Code?
Hello @chip ! This setup works for me : - First I run my project with this command in package.json :
{
"scripts": {
"dev": "tsx watch --inspect --env-file=.env.dev src/main.tsx"
}
}
{
"scripts": {
"dev": "tsx watch --inspect --env-file=.env.dev src/main.tsx"
}
}
If you use other tool than tsxthe important part is --inspect, available on Node.js too
- Then I have this launch.json in .vscode folder :
{
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
}
]
}
{
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
}
]
}
- So you just npm run dev and click on "attach" command in vscode debug tool and your breakpoints should works 🙂 - (Optional) you can auto-attach on npm run dev with this vscode setting :
"debug.javascript.autoAttachFilter": "onlyWithFlag"
"debug.javascript.autoAttachFilter": "onlyWithFlag"
2 replies