Reputation: 11
I'm somewhat new to Grails. As I create or update domain object and fire save()
or validate()
on an object, if the method fails, the system does not seem throw an exception. I dont see any way to examine what exactly is failing.
A typical snippet:
if (domainInstance.validate()) {
flash.message = "Succesfully updated domain object"
} else {
flash.message = "Failed to update domain object"
//throw new RuntimeException("Invalid broker")
log.error "Failed to update domain object"
}
In my case the validate fails, and I am in the dark as to why. Could anybody shed some light on it?
If placed into a try/catch, this does not throw an exception.
Upvotes: 1
Views: 2297
Reputation: 484
You can also set failOnError = true
for the entire application in the grails config file
grails.gorm.failOnError=true
http://www.grails.org/doc/1.3.x/guide/3.%20Configuration.html#3.1.3 GORM
Upvotes: 4
Reputation: 1382
mydomain.validate() is used to only validate the object. You may use mydomain.hasErrors() to load the errors object and to print what went wrong with the following statement.
if(mydomain.hasErrors()){
mydomain.errors.allErrors.each{println it}
}
And generally the way I prefer to save and update any object is
if(mydomain.hasErrors() || !mydomain.save(failOnError:true){
//action to be taken if domain validation fails.
}
By setting failOnError:true, if the save() fails, validation exception would be thrown which needs to catched in controller.
Upvotes: 6