Repository navigation
Description
Original Reporter: mos
Environment: Not Specified
Version: 1.1
Migrated From: http://jira.grails.org/browse/GRAILS-4343
When saving a Domain Object two use-cases exist in general:
- Take doubtful values for object creation (e.g. User-Input)
- Take known and safe values for object creation (e.g. application intern)
The first use-case works well with the existing save() method. You have to check the return value of the save() method and need to handle the error case explicitly.
For the second use-case there is no direct support in Grails. You are always forced to check the return type, if you want to catch an internal error. In practice a lot of people just ignore the return value for this (for example: check posting on mailing-lists or check some plugin source-code). That leads to undetected errors because the save() method is just ignored and people wonder why save() doesn’t work. It’s the contrary of a “fail-fast concept” that’s desirable for robust applications.
Proposal:
- Add a new parameter to the save() method that indicates that an Exception should be thrown if a binding or validation error occurs. For example: obj.save(throw:true)
- Update documentation that either return value needs to be checked or ‘throw:true’ should be declared.
Alternative (even safer):
Create a new and clever save method, that either takes a closure that is executed in the error case. If no closure is declared an Exception would be thrown.
The old save() method needs to be deprecated.
Current Workaround:
In current projects we define an additional Domain Object method that is used for the second use-case (but this one doesn’t help the people out there ;) ):
{code}
ApplicationHolder.application.domainClasses.each { dc →
dc.metaClass.checkAndSave = { m →
if (delegate.hasErrors()) {
throw new IllegalArgumentException("Object of ‘${delegate.class.simpleName}’ couldn’t be saved because of validation errors: “+errors.toString())
}
// check for the flush:true parameter
if (m!=null && m[‘flush’]) {
def sessionFactory = ctx.getBean(”sessionFactory")
sessionFactory.currentSession.flush()
}
}
}
{code}
Activity
graemerocher commentedon Jul 9, 2009
brownj said:
save(failOnError: true) is now supported. If failOnError is set to true and validation fails, a grails.validation.ValidationException is thrown.
graemerocher commentedon Jul 9, 2009
brownj said:
mos: I think changing the default behavior is out of the question. That would be a significant breaking change.
graemerocher commentedon Jul 10, 2009
brownj said:
I will add some support for a property that enables the failOnError behavior but I think the default value for this property needs to be false. I think that a user needs to go out of their way to enable this.
graemerocher commentedon Jul 10, 2009
mos said:
Thanks Graeme and Jeff for clarification. I get your point.
graemerocher commentedon Aug 17, 2009
brownj said:
In 1.2M2 the property was named grails.gorm.save.failOnError. In 1.2M3 the property has been renamed grails.gorm.failOnError. The word "save" has been removed as we may use this same property to effect methods other than save().
graemerocher commentedon Sep 25, 2009
mos said:
The exception message for fail-fast should be improved. Please check: GRAILS-5146
graemerocher commentedon Oct 1, 2009
mos said:
It should be possible to specify "grails.gorm.failOnError" on package level, to avoid conflicts with plugins. Please check: GRAILS-5172
graemerocher commentedon Oct 11, 2009
mos said:
FailOnError configuration isn't applied during validation checks on flushing. Please check: GRAILS-5214
Merge remote-tracking branch 'origin/7.0.x' into fix/release-drafter-…
fix(ci): comprehensive overhaul of release-drafter setup