H
Hono•4mo ago
Augustin Sorel

Error per route

Hello everyone, this is a very basic question but I couldn't find any help online. I was just wondering if it was possible to have any error handling callback per route. For example with this code, how would you handle this error only for this route ? I know I can use .onError but that would be global right ? Any help is very much appricated 🙌
app.get('/idk', (c)=>{
if(Math.random()>0.5){
throw new Error('idk')
}
return c.text('success', 200);
})
app.get('/idk', (c)=>{
if(Math.random()>0.5){
throw new Error('idk')
}
return c.text('success', 200);
})
5 Replies
Kaelwd
Kaelwd•4mo ago
In middleware you can check c.error after await next()
Augustin Sorel
Augustin SorelOP•4mo ago
but wouldn't this be global ? Since it's a middleware
Kaelwd
Kaelwd•3mo ago
no .get('/idk', errorHandler, c => {...
Augustin Sorel
Augustin SorelOP•3mo ago
nice thank you very much this is making more sense. Out of interest do you know why this cb has to be definied before the actual handler ?
Kaelwd
Kaelwd•3mo ago
Because that's the order they're called in
Want results from more Discord servers?
Add your server