grails / grails-core

The Grails Web Application Framework
http://grails.org
Apache License 2.0
2.78k stars 950 forks source link

Problem With failOnError:true #11768

Closed abhishek-nexthoughts closed 3 years ago

abhishek-nexthoughts commented 3 years ago

I am using Grails 4.0.4.I have created a normal domain and trying to update in a transactional service with flush true.It is not generating any error while using failOnError:true.Not able to find any way to fix that,neither cause of it.Please Let me know what I am missing.

jeffscottbrown commented 3 years ago

failOnError: true is supposed to cause an exception to be thrown if any domain constraints are violated. Are you saying that isn't happening?

jeffscottbrown commented 3 years ago

See the project at https://github.com/jeffbrown/abhishekissue11768.

https://github.com/jeffbrown/abhishekissue11768/blob/d1934422d7bd24f68ad061f03bbd82624a331349/grails-app/domain/abhishekissue11768/Person.groovy

package abhishekissue11768

class Person {
    String firstName
    static constraints = {
        firstName matches: /[A-Z].*/
    }
}

https://github.com/jeffbrown/abhishekissue11768/blob/d1934422d7bd24f68ad061f03bbd82624a331349/grails-app/services/abhishekissue11768/PersonService.groovy

package abhishekissue11768

import grails.gorm.transactions.Transactional

@Transactional
class PersonService {

    Person savePerson(String name) {
        new Person(firstName: name).save(failOnError: true)
    }
}

https://github.com/jeffbrown/abhishekissue11768/blob/d1934422d7bd24f68ad061f03bbd82624a331349/src/test/groovy/abhishekissue11768/PersonServiceSpec.groovy

package abhishekissue11768

import grails.testing.gorm.DataTest
import grails.testing.services.ServiceUnitTest
import grails.validation.ValidationException
import spock.lang.Specification

class PersonServiceSpec extends Specification implements ServiceUnitTest<PersonService>, DataTest {

    Class<?>[] getDomainClassesToMock() {
        [Person]
    }

    void "test a valid name"() {
        when:
        service.savePerson 'Jeff'

        then:
        noExceptionThrown()
    }

    void "test an invalid name"() {
        when:
        service.savePerson 'jeff'

        then:
        thrown(ValidationException)
    }
}

Those tests pass. Is this code representative of what you are trying to do?

jeffscottbrown commented 3 years ago

FYI... The whole description of the problem was in the title of this issue. I have shortened the title and pasted the original title into the body of the question.

abhishek-nexthoughts commented 3 years ago

Yes.It is not producing an error.the domain is showing updated.But not reflected over DB

jeffscottbrown commented 3 years ago

Yes.It is not producing an error.the domain is showing updated.

If you can share a sample app which demonstrates how to trigger the problematic behavior, that would be helpful.

puneetbehl commented 3 years ago

Please feel free to reopen once you share a sample application.