diff --git a/errors.go b/errors.go index 470c97d5..5cf29c61 100644 --- a/errors.go +++ b/errors.go @@ -1,6 +1,9 @@ package gorm -import "errors" +import ( + "errors" + "strings" +) var ( RecordNotFound = errors.New("record not found") @@ -9,3 +12,23 @@ var ( NoValidTransaction = errors.New("no valid transaction") CantStartTransaction = errors.New("can't start transaction") ) + +type Errors struct { + errors []error +} + +func (errs Errors) Errors() []error { + return errs.errors +} + +func (errs *Errors) Add(err error) { + errs.errors = append(errs.errors, err) +} + +func (errs Errors) Error() string { + var errors = []string{} + for _, e := range errs.errors { + errors = append(errors, e.Error()) + } + return strings.Join(errors, "; ") +} diff --git a/main.go b/main.go index ae406b8d..376156a8 100644 --- a/main.go +++ b/main.go @@ -258,9 +258,9 @@ func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB { if !result.RecordNotFound() { return result } - c.err(c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates).db.Error) + c.AddError(c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates).db.Error) } else if len(c.search.assignAttrs) > 0 { - c.err(c.NewScope(out).InstanceSet("gorm:update_interface", s.search.assignAttrs).callCallbacks(s.parent.callback.updates).db.Error) + c.AddError(c.NewScope(out).InstanceSet("gorm:update_interface", s.search.assignAttrs).callCallbacks(s.parent.callback.updates).db.Error) } return c } @@ -339,27 +339,27 @@ func (s *DB) Begin() *DB { if db, ok := c.db.(sqlDb); ok { tx, err := db.Begin() c.db = interface{}(tx).(sqlCommon) - c.err(err) + c.AddError(err) } else { - c.err(CantStartTransaction) + c.AddError(CantStartTransaction) } return c } func (s *DB) Commit() *DB { if db, ok := s.db.(sqlTx); ok { - s.err(db.Commit()) + s.AddError(db.Commit()) } else { - s.err(NoValidTransaction) + s.AddError(NoValidTransaction) } return s } func (s *DB) Rollback() *DB { if db, ok := s.db.(sqlTx); ok { - s.err(db.Rollback()) + s.AddError(db.Rollback()) } else { - s.err(NoValidTransaction) + s.AddError(NoValidTransaction) } return s } @@ -389,7 +389,7 @@ func (s *DB) HasTable(value interface{}) bool { scope := s.clone().NewScope(value) tableName := scope.TableName() has := scope.Dialect().HasTable(scope, tableName) - s.err(scope.db.Error) + s.AddError(scope.db.Error) return has } @@ -508,3 +508,28 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join } } } + +func (s *DB) AddError(err error) error { + if err != nil { + if err != RecordNotFound { + if s.logMode == 0 { + go s.print(fileWithLineNum(), err) + } else { + s.log(err) + } + + err = Errors{errors: append(s.Errors(), err)} + } + + s.Error = err + } + return err +} + +func (s *DB) Errors() []error { + if errs, ok := s.Error.(Errors); ok { + return errs.errors + } else { + return []error{s.Error} + } +} diff --git a/main_private.go b/main_private.go index 84f10e35..bd097ce0 100644 --- a/main_private.go +++ b/main_private.go @@ -19,20 +19,6 @@ func (s *DB) clone() *DB { return &db } -func (s *DB) err(err error) error { - if err != nil { - if err != RecordNotFound { - if s.logMode == 0 { - go s.print(fileWithLineNum(), err) - } else { - s.log(err) - } - } - s.Error = err - } - return err -} - func (s *DB) print(v ...interface{}) { s.logger.(logger).Print(v...) } diff --git a/scope.go b/scope.go index 2af95566..7b6764a6 100644 --- a/scope.go +++ b/scope.go @@ -103,7 +103,7 @@ func (scope *Scope) Dialect() Dialect { // Err write error func (scope *Scope) Err(err error) error { if err != nil { - scope.db.err(err) + scope.db.AddError(err) } return err } diff --git a/search.go b/search.go index ee3d494c..166b9a86 100644 --- a/search.go +++ b/search.go @@ -139,7 +139,7 @@ func (s *search) getInterfaceAsSql(value interface{}) (str string) { case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: str = fmt.Sprintf("%v", value) default: - s.db.err(InvalidSql) + s.db.AddError(InvalidSql) } if str == "-1" {