When persisting domain objects using Grails/GORM I frequently find myself wondering why a save() call fails.
This can easily be solved by adding the logic:
if (!o.save()) {
o.errors.allErrors.each { println it }
}
However, adding this everywhere I do a .save() adds a lot of duplicate code. In the spirit of DRY I'd like to configure Grails/GORM to automatically print any save-errors to the console (stderr). Is that possible? If not, how do I extend GORM to make it possible?
Solution:
Object.metaClass.s = {
def o = delegate.save()
if (!o) {
delegate.errors.allErrors.each {
println it
}
}
o
}
This adds a method called s() that will call save() and print any errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With