404 Handler Example?

2,235 views
Skip to first unread message

Oje Preradovic

unread,
Jul 23, 2015, 8:32:56 PM7/23/15
to vert.x
The docs just say:

Default 404 Handling

If no routes match for any particular request, Vert.x-Web will signal a 404 error.

This can then be handled by your own error handler, or perhaps the augmented error handler that we supply to use, or if no error handler is provided Vert.x-Web will send back a basic 404 (Not Found) response.

Anyone have a quick example of how to hook into a 404??

Arnaud Estève

unread,
Jul 24, 2015, 5:08:58 AM7/24/15
to vert.x, gro...@gmail.com
I think one of the things you could try would be : 

router.route().failureHandler(failedRoutingContext -> {
  if (failedRoutingContext.statusCode() == 404) {
    // do what you want
  }
});

Oje Preradovic

unread,
Jul 24, 2015, 11:05:56 AM7/24/15
to vert.x, arnaud...@gmail.com
Did that never gets called for 404. I am assuming that the default 404 handler is running and taking those calls? How do u uninstall default? Or hook into it??

Arnaud Estève

unread,
Jul 24, 2015, 11:13:53 AM7/24/15
to vert.x, gro...@gmail.com
My bad, I did that kind of stuff only for "views not found" (i.e. template engine failling).

Maybe you could just try to declare router.route().handler(context -> { /* handle 404 here */}) as the last declaration of your handlers. 

If it gets called, that would mean no other handler did match, so... 404 ?

Maybe this is an option to consider ?

Timm Baumeister

unread,
Jan 28, 2016, 1:30:05 PM1/28/16
to vert.x, gro...@gmail.com
Is there no way to do this with an error handler? I want to be able to dynamically add and remove routes via plugins. Needing to keep a 404 handler at the end of my routing list and thus depending on the sequence how routes were added makes this difficult.

http://vertx.io/docs/vertx-web/js/#_default_404_handling sounds as if it should be possible, without example though it's not clear to me.  Using failureHandler doesn't work.

Asher Tarnopolski

unread,
Jan 29, 2016, 8:30:51 AM1/29/16
to vert.x, gro...@gmail.com

what about something like

router.route().last().handler( ( RoutingContext context ) -> {
          context
.response().setStatusCode( 404 ) .end();
     
} );

doesn't  last()  keep its  hanlder last in the routing order in any case?

Timm B

unread,
Jan 31, 2016, 9:22:19 PM1/31/16
to vert.x, gro...@gmail.com
It does. Thanks for the tip!

Oje Preradovic

unread,
Feb 12, 2016, 2:12:32 PM2/12/16
to vert.x, gro...@gmail.com
Thank you! Works great!
Reply all
Reply to author
Forward
0 new messages