Skip to content

Commit

Permalink
Add Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Aug 14, 2015
1 parent e1ce3b7 commit 3097409
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
25 changes: 24 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gorm

import "errors"
import (
"errors"
"strings"
)

var (
RecordNotFound = errors.New("record not found")
Expand All @@ -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, "; ")
}
43 changes: 34 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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}
}
}
14 changes: 0 additions & 14 deletions main_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
2 changes: 1 addition & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 3097409

Please sign in to comment.