Updated On: 2 Jul 2008 - Tested on Grails 1.0.1
Grails allows you to perform custom , fine-grained Exception handling for error conditions in your controllers. As per usual, you can take advantage of this feature by making use of Grails' nifty Dependency Injection features. In this case, we will depend on the GrailsExceptionResolver class being injected into /conf/BootStrap.groovy as "exceptionHandler".
Note: This article shows you a fine-grained alternative to the "500 Internal Server" catch-all which is generated by default in /conf/UrlMappings.groovy
The important changes are in the below bit of code on lines 3, and 7 to 9 of my BootStrap.groovy file:
1:class BootStrap {
2:
3: def exceptionHandler
4:
5: def init = { servletContext ->
6:
7: exceptionHandler.exceptionMappings =
8: [ 'NoSuchFlowExecutionException' :'/my/doIt',
9: 'java.lang.Exception' : '/error']
10:
11: }
12:
13: def destroy = { }
14:}
In the above code snippet we set the exceptionHandler's exceptionMappings property to a Map containing the name of the exception we wish to handle, mapped to the URI we wish to handle our error condition.
Line 9 above is very important. This is our "catchall" for all other java.lang.Exceptions to be handled by Grails' internal error handler. It is worth noting that leaving out "java.lang" will have the incorrect bevaviour. Taken from the javadoc for GrailsExceptionResolver's setExceptionMappings() method:
NB: Consider carefully how specific the pattern is, and whether to include package information (which isn't mandatory). For example, "Exception" will match nearly anything, and will probably hide other rules. "java.lang.Exception" would be correct if "Exception" was meant to define a rule for all checked exceptions. With more unusual exception names such as "BaseBusinessException" there's no need to use a FQN.
NOTE: In the above code the URI maps onto our urlMappings, therefore
- '/my/doIt' - invokes doIt closure on MyController
- '/error' - skips straight to /views/error.gsp
Lastly, in order for all the above to work, we must remove the default "500" error handler from your /conf/UrlMappings.groovy as follows (line 8):
1:class UrlMappings {
2: static mappings = {
3: "/$controller/$action?/$id?"{
4: constraints {
5: // apply constraints here
6: }
7: }
8:// "500"(view: null)
9: }
10:}
- Spring 2.5 API Documentation for SimpleMappingExceptionResolver
- Grails 1.0.1 API Document for GrailsExceptionResolver