HowTo use error pages

From Lift

Jump to: navigation, search

At some point when building web application the question of how do we handle errors, how do we use error pages when something unexpected happens, arises. Also having a centralized place to handle such cases is very tempting.

With Lift this is very easy to achieve:

In Boot class add something like:

   LiftServlet.browserResponseToException = {
           case (mode, state, ex) => RedirectResponse("/error", state)
   }

browserResponseToException is a PartialFunction that is called when Lift catches an unexpected exception. RedirectResponse allows you to redirect to a lift template (or any other URL for that matter. If uri starts with / it is considered a relative path to the current context path). We use the HTTP redirect approach instead of dynamically load a template due to safety reasons and let lift process the error page in the normal render pipeline.

Note that if you are using the SiteMap the error page URI must be added as a SiteMap entry.

You can use RedirectResponse for LiftServlet.DispatchPF as well.

If you don't want to use RedirectResponse in the cases presented above you can use any other ResponseIt class such as (XhtmlResponse, XmlResponse etc)

Br's, Marius

Personal tools