Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This guideline provide you with how to have it and how it has been made so that you can easily use it and customize it based on your practical situation.

...

After successfully installed the module, when accessing a nonexistence page such as http://localhost:8080/travel/ abc, you will have this:

Image Added

Set up steps

Setting Servlet Container exception handling page

First of all in order to make our web-container such as Apache Tomcat aware of our customization, we would have to put snipped of code this to our ''Tomcat/webapps/YOUR-MAGNOLIA-WEBAPP/WEB-INF/web.xml' file just above the closing of 'weweb-app' tag (above this "</web-app>").

...

Please note that we are using a specific location "/.exception" for all every "java.lang.Exception' ones, you could also change it in case of conflict with any of your existing one. Later on we will show where the mapping is. So if you change it here, you would also have to change the corresponding one for direct mapping of exception handling page.

Put the pre-built module to your 'Tomcat/webapps/YOUR-MAGNOLIA-WEBAPP/WEB-INF/lib' folder then restart your server and install the new module.

Download here: mgnlsupport-1.0-SNAPSHOT.jar

Warning

Please change / update module dependencies based on your webapp

Using and Configuring

After successfully installed the module, when accessing a nonexistence page such as http://localhost:8080/travel/ abc, you will have this:

Configuring your error page template - Optional

After previous step, you already configured Servlet container to render your exception in your specified page / servlet called "/.exception".

Warning

Please be careful that within your exception page, if you also having the same kind of exception, a cyclic reference will happen which might lead to a "stack overflow" error while rendering the exception of the exception page (wink)

This step is optional because you can use any of your existing template / page as an exception page.Image Removed

All the configuration points should be located under our pre-built any of your module or light module such as 'mgnlsupport' module belowin this case:

Basically we provided a default exception page template, its configuration is under our module 'templates/pages/exception' node as any other template configuration. You can reference to our official documentation for template configuration guidelines.

Creating different exception pages

Because we need to provide Also we provided 2 site-aware exception mappings (in this example) which have been configured out of the box pointing to 2 default exception pages for our 'travel' demo site and the 'fallback' one.

You should be able to locate our So you should create 2 default exception pages under our pages app as below image and assign them using your configured template(s).

Image Added

Info

Your exception information follow Java Servlet Specification will be stored within current request attribute named "javax.servlet.error.exception".

Using Magnolia Freemarker renderer (FTL file) you can call ${ctx.request.getAttribute("javax.servlet.error.exception") to get it. Some other error fields that you can get from Servlet API:

ServletRequestjavax.servlet.error.status_codeIntegerfor error pages only: HTTP status code
ServletRequestjavax.servlet.error.exception_typeClassfor error pages only: exception class
ServletRequestjavax.servlet.error.messageStringfor error pages only:

...

error message
ServletRequestjavax.servlet.error.exceptionThrowablefor error pages only: exception
ServletRequestjavax.servlet.error.request_uriStringfor error pages only: original request URI
ServletRequestjavax.servlet.error.servlet_nameStringfor error pages only: servlet name

Image Removed

...

Changing exception page location

...

Then just follow our light-dev guideline, provide your own template and point to to appropriate classpath folder then the new FTL file will be used for our configured template.

Here is an example exception.ftl file.

Implementing another kind of mapping or exception handling

...

Code Block
languagejava
titleSiteAwareExceptionMapping
linenumberstrue
collapsetrue
package info.magnolia.virtualuri.mapping;

import java.net.URI;
import java.util.Optional;
import info.magnolia.cms.util.UrlPattern;

public class SiteAwareExceptionMapping extends DefaultVirtualUriMapping {
    
    public static final String SERVLET_ERROR_REQUEST_URI_ATTRIBUTE_NAME = "javax.servlet.error.request_uri";
    
    private String siteName;
    private String toUri;
    
    @Override
    public Optional<Result> mapUri(URI uri) {
        UrlPattern pattern = getPattern();
        if (pattern != null && pattern.match(uri.getPath())) {
            return Optional.of(new Result(toUri, toUri.length(), this));
        } 
        return Optional.empty();
    }

    public String getSiteName() {
        return siteName;
    }

    public void setSiteName(String siteName) {
        this.siteName = siteName;
    }

    public String getToUri() {
        return toUri;
    }

    public void setToUri(String toUri) {
        this.toUri = toUri;
    }
    
}

...

This is an upgraded version of How to setup a custom 404 handler as of January 2018 2019 because the previous one has been out dated and did not maintained for such a long time. We're trying to bring values to customers who are using and contributing to Magnolia CMS.

...