Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(hono): not found handled by sub #3090

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

douglasduteil
Copy link

@douglasduteil douglasduteil commented Jul 4, 2024

Hi

I don't know if it's a typo but I expect a sub not found handler to take priority over the parent not found.

Case :

const app = new Hono()
const sub = new Hono()
app.notFound((c) => {
  return c.text('404 Not Found by app', 404)
})
sub.notFound((c) => {
  return c.text('404 Not Found by sub', 404)
})
app.route('/sub', sub)

@yusukebe
Copy link
Member

yusukebe commented Jul 5, 2024

Hi @douglasduteil

It's not a typo or mistake. It's intended. The app.route('/sub', sub) is just adding the sub app's routes to the main app, not passing the not found handler.

If you want to handle a 404, you can create a fallback endpoint:

sub.get('/ok', (c) => {
  return c.text('ok')
})

// ...

sub.all('*', (c) => {
  return c.text('404 Not Found by sub', 404)
})

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Jul 5, 2024

I understand this spec, but I believe the notFoundHandler should take over as well.
I think this is more elegant than creating endpoints for all paths.  
I believe this could be accomplished by creating a route in .route that passes app.all("*", (c) => subApp.notFoundHandler()) with a notFoundHandler.

I think this will improve the user experience.

However, currently notFoundHandler is a private variable

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Jul 5, 2024

image

I think that this is a PoC that does not consider already registered routes, it does not pass the other tests,
but the features mentioned in this PR can be implemented with my proposed approach.

@EdamAme-x
Copy link
Contributor

The implementation is simple, since all you have to do is search for the presence of /* in the already registered routes. If this proposal is accepted I can send a PR to this PR branch.

@EdamAme-x
Copy link
Contributor

I think there are two points to discuss on this. "Should we make notFoundHandler public?" and "type inference"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants