Skip to content

GRAILS-4343: Add a safe save() method to Domain Objects #1593

Closed

Description

@graemerocher

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

  1. graemerocher commented on Jul 9, 2009

    @graemerocher
    ContributorAuthor

    brownj said:
    save(failOnError: true) is now supported. If failOnError is set to true and validation fails, a grails.validation.ValidationException is thrown.

  2. graemerocher commented on Jul 9, 2009

    @graemerocher
    ContributorAuthor

    brownj said:
    mos: I think changing the default behavior is out of the question. That would be a significant breaking change.

  3. graemerocher commented on Jul 10, 2009

    @graemerocher
    ContributorAuthor

    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.

  4. graemerocher commented on Jul 10, 2009

    @graemerocher
    ContributorAuthor

    mos said:
    Thanks Graeme and Jeff for clarification. I get your point.

  5. graemerocher commented on Aug 17, 2009

    @graemerocher
    ContributorAuthor

    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().

  6. graemerocher commented on Sep 25, 2009

    @graemerocher
    ContributorAuthor

    mos said:
    The exception message for fail-fast should be improved. Please check: GRAILS-5146

  7. graemerocher commented on Oct 1, 2009

    @graemerocher
    ContributorAuthor

    mos said:
    It should be possible to specify "grails.gorm.failOnError" on package level, to avoid conflicts with plugins. Please check: GRAILS-5172

  8. graemerocher commented on Oct 11, 2009

    @graemerocher
    ContributorAuthor

    mos said:
    FailOnError configuration isn't applied during validation checks on flushing. Please check: GRAILS-5214

  9. added this to the grails-1.2-M2 milestone on May 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions