Friday, 4 March 2011

Custom error page for your grails application

I recently added a custom error page to my application, so that the users don't see the stacktrace and are shown a friendly message in case of a runtime error.



I simply added a controller that checks for the environment in which the application is running, and accordingly renders different views:

import grails.util.*;

class ErrorsController {
    def serverError = {
        def env = GrailsUtil.environment;

        if(env == "production")
            render(view:'/serverError')
        else
            render(view:'/error')
    }

    def notFound = {
        render(view:'/notFound')
    }
}
Added a new view file 'serverError.gsp' at \grails-app\views:
<html>
    <head>
        <title>Error on the server</title>
        <meta name="layout" content="main" />
    </head>
    <body>

      <h1 style="margin-left:20px;color:#006dba;" >
            An unexpected error occurred while processing your request. The admin team has been notified.             <br/>
            Please try again, or
              <a href="mailto:abc@xyz.com?
              subject= [${GrailsUtil.environment}] Application Error occured : '${exception?.message?.encodeAsHTML()}'">
              contact
              </a> the IT team

      </h1>

    </body>
</html>
To delegate the request to the ErrorController, add the following line to the UrlMappings.groovy file:
"500"(controller: "errors", action:"serverError") .

Handling 404 Not Found:

If you want to handle the 404 Not Found situation specifically, add the following line to your UrlMappings.groovy file (which you can find at \grails-app\conf)
class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
         "500"(controller: "errors", action:"serverError")
         "404"(controller:"errors", action:"notFound")
    }
}
The view file notFound.gsp looks like this:

<html>
    <head>
        <title>Page not found</title>
        <meta name="layout" content="main" />
    </head>
    <body>

       <h1 style="margin-left:20px;">
         <p style="margin-left:20px;width:80%">
            Page Requested not found, please check the URL
         </p>
       </h1>
    </body>
</html>

5 comments:

  1. Nivista is Custom Software Development Company offers services in .NET Application Development, Dynamic CRM, Sharepoint Application Developement, Business Intelligence Reporting, OffShore Software Development etc. We are expertise in providing IT and ITES solutions. Custom Software Development Company

    ReplyDelete
  2. Thanks for helpful tips and nice review. Well done. To make somes business improvements, usually it uses custom software development company that meets various business expectations and needs in software development.

    ReplyDelete
  3. I was looking for it, its perfect idea to show custom pages for errors.

    Great.

    ReplyDelete