Gracefully handling 404s with Lift
In my continuing effort to improve the wider knowledge of Lift, here’s another great nugget that shows you how to provide a 404 handler powered from a Lift template
class Boot {
def boot {
// ...other stuff...
LiftRules.uriNotFound.prepend(NamedPF("404handler"){
case (req,failure) =>
NotFoundAsTemplate(ParsePath(List("404"),"html",false,false))
})
}
}
And I have a 404.html file in the ./webapp/404.html with the contents:
<lift:surround with="default" at="content">
<h2>Ooops!</h2>
<p>Wow, we totally couldn't find your page. Sorry about that.</p>
</lift:surround>
Then whenever a user browses to a URL that is not supported by your application, you can give them a friendly (and nicely designed) webpage rather than some technical message about 404s.
Note: The only cavet is not to use the <lift:menu />
snippet within your 404 template because Lift is unable to calculate the SiteMap when its a URL that is not accounted for within that scheme.
UPDATE The above note is now redundant because of commit by Marius on issue 376