DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

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

Jianfeng Tian user avatar by
Jianfeng Tian
·
Aug. 05, 10 · Interview
Like (0)
Save
Tweet
Share
27.29K Views

Join the DZone community and get the full member experience.

Join For Free

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



${it.encodeAsHTML()}


...

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:

Grail (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Create a Failover Client Using the Hazelcast Viridian Serverless
  • 5 Steps for Getting Started in Deep Learning
  • Unlock the Power of Terragrunt’s Hierarchy
  • Shift-Left: A Developer's Pipe(line) Dream?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: