Over a million developers have joined DZone.

Grails Exception Handling with HTTP Status Code, URLMapping, and Email

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

The post describes how to:

1. Configure the UrlMapping.groovy to allow custom action on the specific http status code.
2. Display a customised "https status code handling page" to the user.
3. Send customised "https status code handling page" as an email to the admin staff if the admin email is configured in the Config.groovy file.

1. Configure the UrlMapping.groovy

class UrlMappings {
static mappings {
"403" (controller: "error", action: "forbidden")
"404" (controller: "error", action: "notFound")
"500" (controller: "error", action: "internalError")

So any page status with 500 will be handled by the internalError action in the error controller.

Individual exceptions can be also configured in the response code to give more fine grained level control
  "500" (controller: "error", action: "nullPointer", exception: NullPointerException)
"500" (controller: "error", action: "illegalArgument", exception: IllegalArgumentException)

This allows any NullPointerException with 500 http status code to be handled by nullPointer action of error controller.

2. Display a customised "https status code handling page" to the user.

There are two properties available to the exception handling page. These properties are: exception and request. So you can copy the default error.gsp in the grails-app/view folder to a new file which is specified in the UrlMapping.groovy (i.e. error/internalError.gsp) and modified it to your needs.
Error ${request.'javax.servlet.error.status_code'}: ${request.'javax.servlet.error.message'.encodeAsHTML()}

Servlet: ${request.'javax.servlet.error.servlet_name'}

URI: ${request.'javax.servlet.error.request_uri'}


Stack Trace



The error page is as below:

3. Send customised "https status code handling page" as an email to the admin staff

The grails mail plugin is required to fulfil this requirement. Please refer to the Grail Mail plugin documentation

The following sending email code needs to be placed in the relevant action specified in the UrlMapping.groovy file.

import org.codehaus.groovy.grails.commons.ConfigurationHolder

class ErrorController {

def mailService // mailService is provided by the grails mail plugin

def index = { }

def internalError = {
// admin email is specified in the Config.groovy file
// An email will be sent to the admin person whenever an internalError occurred.
def adminEmail = ConfigurationHolder.config.admin.email
try {
mailService.sendMail {
multipart true
to adminEmail
subject "500 Error"
body (view: "/email/error")
catch (Exception e) {
log.info e

def notFound = {}

The exception and request properties in the exception handling page are also available in the email template page. Thus the default error.gsp can be also copied to email template page (i.e. grails-app/email/error.gsp) and modified to your needs.

The error email is as below:

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}