Skip to content

Instantly share code, notes, and snippets.

@joeRinehart
Created November 29, 2012 08:51
Show Gist options
  • Save joeRinehart/4167671 to your computer and use it in GitHub Desktop.
Save joeRinehart/4167671 to your computer and use it in GitHub Desktop.
Grails Error Handling
grails> create-controller Error
<!doctype html>
<html>
<head>
<title>Application Error - Development View</title>
<meta name="layout" content="bootstrap">
<meta name="bootstrap" content="main">
<link rel="stylesheet" href="${resource(dir: 'css', file: 'errors.css')}" type="text/css">
</head>
<body>
<p><em>This is a development view of an error, and would not be shown in production.</em></p>
<g:renderException exception="${exception}" />
</body>
</html>
import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil
class ErrorController {
def index() {
if ( GrailsApplication.ENV_PRODUCTION == GrailsUtil.environment ) {
// Production: show a nice error message
render(view:'production')
} else {
// Not it production? show an ugly, developer-focused error message
render(view:'development')
}
}
}
import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil
class ErrorController {
def index() {
// "Do stuff" with the exception:
Exception exception = request.exception
println "Here's what happened: ${exception}"
if ( GrailsApplication.ENV_PRODUCTION == GrailsUtil.environment ) {
// Production: show a nice error message
render(view:'production')
} else {
// Not it production? show an ugly, developer-focused error message
render(view:'development')
}
}
}
import org.codehaus.groovy.grails.commons.GrailsApplication
import grails.util.GrailsUtil
class ErrorController {
def index() {
try {
// "Do stuff" with the exception:
Exception exception = request.exception
println "Here's what happened: ${exception}"
if ( GrailsApplication.ENV_PRODUCTION == GrailsUtil.environment ) {
// Production: show a nice error message
render(view:'production')
} else {
// Not it production? show an ugly, developer-focused error message
render(view:'development')
}
} catch ( Exception e ) {
render(view: 'unhandleable')
}
}
}
<!doctype html>
<html>
<head>
<title>Application Error</title>
<meta name="layout" content="bootstrap">
<meta name="bootstrap" content="main">
<link rel="stylesheet" href="${resource(dir: 'css', file: 'errors.css')}" type="text/css">
</head>
<body>
<h1>Application Error</h1>
<p>We're sorry, but the application has encountered an error. Please try again.</p>
</body>
</html>
class UrlMappings {
static mappings = {
/* other stuff.... */
"500"(controller: "error")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment