This page has been updated to comply with Magnolia 5.7.+ as of February 2019, previous version was written for Magnolia older versions.
Introduction
There have been more and more multi-site users of Magnolia CMS asking us for a customizable, multi-language enabled, site aware and out of the box solution from Magnolia CMS to support web container servlet exception handling. As a multi-site user of Magnolia CMS, you can have multiple sites configured and mapped by multiple URLs / Domains / Hosts. Whenever an issue happen such as Resource not found (HTTP 404 status code) or server error (500 internal server error), web container will redirect us to its default error report page. If we don't have corresponding configuration, we cannot have a nice error report to our valued customers.
A typical example is when end user calling this link https://demopublic.magnolia-cms.com/travel/ where is my tour?
This is what we get from a non-configured site
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:
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 'web-app' tag (above this "</web-app>").
<error-page> <error-code>404</error-code> <location>/customException</location> </error-page> <error-page> <error-code>500</error-code> <location>/customException</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/customException</location> </error-page>
Please note that we are using a specific location "/customException" for all every "java.lang.Throwable' 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.
Deploy Java implementation of site aware exception handling
If you are developing a custom Magnolia Maven module, then just make below class available in your classpath.
Sample Java code
Class: info.magnolia.virtualuri.mapping.SiteAwareExceptionMapping
This source code SiteAwareExceptionMapping.java is provided without any guarantee as a community creative & publicly available one. Please use it with cares.
Deployment for light-dev and on Magnolia Cloud
Customers of Magnolia light-dev and Magnolia Cloud could also deploy a Java class into Magnolia using Groovy Classes follow similar to our guideline here Model Class
1._Create corresponding folder structure and a SiteAwareExceptionMapping file using Magnolia Groovy App as below image:
2._Edit the empty file and put above Java source code inside.
3._Be aware of this issue (and workaround) when registering new classes: MGNLGROOVY-148 - Creating a new "groovy class" always fails with compliation error OPEN
(work around: click the "Is a script?" checkbox when editing the file, save. This allows the node to be created for the script. Edit the script and uncheck the box.)
→ Above steps make the class available in your class path without custom module or additional deployment efforts.
Configure mappings for your error locations
After previous step, you already configured Servlet container to render your exception in your specified page / servlet called "/customException".
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
Please use either YAML configuration or JCR configuration, do not use both of them to prevent duplication.
YAML configuration
Under your light module, create folder name 'virtualUriMappings' and put your configured mappings inside such as:
sportstation-exception-mapping.yaml
fallback-exception-mapping.yaml
Sample content for your convenience, please change the mapping detail based on your needs:
class: "info.magnolia.virtualuri.mapping.SiteAwareExceptionMapping" fromUri: "/customException" toUri: "redirect:/travel/exception" siteName: "travel"
JCR configuration
All the configuration points should be located under any of your module or light module such as 'mgnlsupport' module in 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 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.
So you should create 2 default exception pages under our pages app as below image and assign them using your configured template(s).
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:
ServletRequest | javax.servlet.error.status_code | Integer | for error pages only: HTTP status code |
ServletRequest | javax.servlet.error.exception_type | Class | for error pages only: exception class |
ServletRequest | javax.servlet.error.message | String | for error pages only: error message |
ServletRequest | javax.servlet.error.exception | Throwable | for error pages only: exception |
ServletRequest | javax.servlet.error.request_uri | String | for error pages only: original request URI |
ServletRequest | javax.servlet.error.servlet_name | String | for error pages only: servlet name |
Changing exception page location
As you can easily find that we have this configuration point '/modules/mgnlsupport/virtualUriMappings/travel-exception-mapping@toUri' which has the value as 'redirect:/travel/exception' then if you have another page somewhere for your site, you can easily change the page location from '/travel/exception' to your new one. The working mechanism is described in our official Virtual URI mapping function.
Changing exception page template
We already provided you with a sample (not very fancy) page template within our pre-built package under '/mgnlsupport/src/main/resources/mgnlsupport/templates/pages/exception.ftl' (in this case my module name is mgnlsupport, will change it later when I have time). Then we have a configuration point for it such as
'/modules/mgnlsupport/templates/pages/exception@templateScript=/mgnlsupport/templates/pages/exception.ftl'
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.
Sample FTL code
File exception.ftl
References
This is an upgraded version of How to setup a custom 404 handler as of January 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.
1 Comment
Richard Gange
Is there anyway I could get you to turn this into a module for incubator/forge? If it also came a with a sample for the travel demo that would be amazing.