refactor: translate error only when it is not nil (#6133)

* refactor: translate error only when it is not nil

* refactor: fix the error flow

* refactor: update the error if checks

* Update gorm.go

---------

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
Co-authored-by: Jinzhu <wosmvp@gmail.com>
This commit is contained in:
Saeid Kanishka 2023-03-10 09:51:27 +01:00 committed by GitHub
parent ed474152b1
commit 707d70a542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 7 deletions

View File

@ -347,15 +347,17 @@ func (db *DB) Callback() *callbacks {
// AddError add error to db // AddError add error to db
func (db *DB) AddError(err error) error { func (db *DB) AddError(err error) error {
if err != nil {
if errTranslator, ok := db.Dialector.(ErrorTranslator); ok { if errTranslator, ok := db.Dialector.(ErrorTranslator); ok {
err = errTranslator.Translate(err) err = errTranslator.Translate(err)
} }
if db.Error == nil { if db.Error == nil {
db.Error = err db.Error = err
} else if err != nil { } else {
db.Error = fmt.Errorf("%v; %w", db.Error, err) db.Error = fmt.Errorf("%v; %w", db.Error, err)
} }
}
return db.Error return db.Error
} }