I am trying to determine what the Error Code is from an exception that is thrown when interacting with a domain object in Grails.
I have a database that has some field validations, and one of the validations is that a specific column must be unique. According to the docs it will give an Error Code of className.propertyName.unique(http://grails.org/doc/latest/ref/Constraints/unique.html). When I wrap my controller in a try catch block like. I can catch all kinds of validation exceptions this:
catch (grails.validation.ValidationException e) {
exception handling code here
}
How do I access the Error Code? I would like to do something like If the Error Code = className1.propertyName2.unique, then respond propertyName2 is not unique.
I do have "failOnError: true" set as a parameter when I do my save operation.
Thanks!
className1.propertyName2.unique=propertyName2 is not uniqueinmessages.propertiesfile and grails did you work no need to catch. - MKBdomain objectand it fails, it will do so quietly, but you SHOULD have something in it callederrorsthat will give you the errors. It'll look like this: def domainObject = new DomainObject(params) if(!domainObject.save()){ domainObject.each{ print(it) } } Again that's quick and dirty and not complete, but should get you what you need. - Ted DelezenedomainObject.errors.each{ render it }or whatever you want to do with it... or you could throw your own exception on error, using that same technique, I generally don't handle it that way though, Typically if an object doesn't save properly, I render a specific template that enumerates the errors from the domain object that I pass to it using theeachError tag( grails.org/doc/latest/ref/Tags/eachError.html ) - Ted Delezene