bearhunterUA
bearhunterUA

Reputation: 259

More info about save in GORM

I have a simple block of code, that return false

PromoDebit promoDebit = new PromoDebit();
            promoDebit.promoCode=promoCode;
            promoDebit.userId=userId;
            promoDebit.countUsages=countUsages;
            promoDebit.endDate=endDate;
            promoDebit.startDate=startDate;
            promoDebit.status=1;
            promoDebit.calcValue=Float.parseFloat(p.getProperty("promoPercent"));
             if(promoDebit.save(flush: true)){
                 log.info "GOOD!"
            } else {
                 log.info "BAD!"
             }

How can I get more info about GORM.save fail? Stdrout log show nothning even if I turn on logSql in DataSource.groovy

Upvotes: 0

Views: 54

Answers (2)

Daniel Wondyifraw
Daniel Wondyifraw

Reputation: 7713

First ,make sure that your Log4j is configured correctly.

Here http://grails.org/doc/2.3.1/guide/conf.html#logging

And ,If you wan to know more detail info on GORM.save fail like

        PromoDebit promoDebit = new PromoDebit();
        promoDebit.promoCode=promoCode;
        promoDebit.userId=userId;
        promoDebit.countUsages=countUsages;
        promoDebit.endDate=endDate;
        promoDebit.startDate=startDate;
        promoDebit.status=1;
        promoDebit.calcValue=Float.parseFloat(p.getProperty("promoPercent"));

Then ,

if (!promoDebit.save()) {
   log.warn myDomainObj.errors.allErrors.join(' \n') 
   //each error is an instance of  org.springframework.validation.FieldError    
}

And , I like this one

if (!promoDebit.save()) {
    promoDebit.errors.each {
        println it
    }
}

If you want to throw an exception for EVERY domain classes, then simply set grails.gorm.failOnError to true in grails-app/conf/Config.groovy

or Simply

promoDebit.save(failOnError:true)

Cheers!

Upvotes: 4

injecteer
injecteer

Reputation: 20699

log.warn "error occurred by saving: $promoDebit.errors"

you will see the validation failures in the log. If some SQL-constraint gets broken you will get a full-blown Exception like DataIntegrityException

Upvotes: 1

Related Questions