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:
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)
I simply added a controller that checks for the environment in which the application is running, and accordingly renders different views:
import grails.util.*;Added a new view file 'serverError.gsp' at \grails-app\views:
class ErrorsController {
def serverError = {
def env = GrailsUtil.environment;
if(env == "production")
render(view:'/serverError')
else
render(view:'/error')
}
def notFound = {
render(view:'/notFound')
}
}
<html>To delegate the request to the ErrorController, add the following line to the UrlMappings.groovy file:
<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>
"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 {The view file notFound.gsp looks like this:
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(controller: "errors", action:"serverError")
"404"(controller:"errors", action:"notFound")
}
}
<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>
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.
ReplyDeletethx :)
ReplyDeleteI was looking for it, its perfect idea to show custom pages for errors.
ReplyDeleteGreat.
thank u very much.......
ReplyDeleteCustom software application development - SofHub is one of the leading software design and development company in Arizona. We provide customized software solutions from expert software developers. Please click for more information https://www.sofhub.com/
ReplyDeleteit is avery nice for us.<a href="http://www.chloros.in/customized-software-solution.html>CUSTOMIZED SOFTWARE SOLUTIONS</a>
ReplyDeletecustomized software solution
ReplyDelete