Question

I am building a website with Snap. How do I specify a custom 404 handler to capture all routes that don't exist?

I would like to redefine the default:

No handler accepted "/asdfasdf"

Thanks

Was it helpful?

Solution

The correct way to modify the routing behaviour is by using wrapSite :

wrapSite (\site -> site <|> writeBS "Use your custom 404 handler" )

In the example code, you can add this line in your base Initializer. In Snap Init code, it would be in the do from app function.


Also "" will match for any route given. Example:

routes = [ ("/login",    with auth handleLoginSubmit)
         , ("/logout",   with auth handleLogout)
         , ("/new_user", with auth handleNewUser)
         , ("/static", serveDirectory "static")
         , ("",        writeBS "This if none of the others" )
         ]

You can change the handler for "" to be your custom 404.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top