1
votes

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!

1
just add className1.propertyName2.unique=propertyName2 is not unique in messages.properties file and grails did you work no need to catch. - MKB
I don't have the time to go in full detail and give you a super great answer right now, however, the short and dirty answer is this, that when you try to save the domain object and it fails, it will do so quietly, but you SHOULD have something in it called errors that 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 Delezene
e.getErrors().getFieldError() is an array of 20 errors, and one of the items in the array is the Error Code from the documentation. Should I just check that array each time if that Error Code exists there? --- @user1690588 I do have that set in my messages.properties. Even when I check the array and print it out, it does not change it from lassName1.propertyName2.unique to propertyName2 is not unique --- TedDelezene, I have it set to fail if it didn't work. Do you know if there is a way to do it with an exception? - Alex Sneed Miller
You may still have the error list in the original domain object after you catch the exception so you should be able to handle the errors inside in the same manner domainObject.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 the eachError tag ( grails.org/doc/latest/ref/Tags/eachError.html ) - Ted Delezene
Thanks for your help, I ended up doing basically that (see post below) - Alex Sneed Miller

1 Answers

3
votes

The Error Code is burried deep within the object. It will be one of the items in the list that is returned by calling the following code, where 'e' is the exception object.

e.getErrors().getFieldError()

You can also get just the code ("unique" in this case) from the exception by calling the following:

e.getErrors().getFieldError().getCode()