Add errorsInterface

This commit is contained in:
Jinzhu 2015-08-14 14:53:26 +08:00
parent 0996ddb604
commit dd0d4d931f
2 changed files with 14 additions and 5 deletions

View File

@ -13,6 +13,10 @@ var (
CantStartTransaction = errors.New("can't start transaction")
)
type errorsInterface interface {
GetErrors() []error
}
type Errors struct {
errors []error
}

15
main.go
View File

@ -518,7 +518,11 @@ func (s *DB) AddError(err error) error {
s.log(err)
}
err = Errors{errors: append(s.GetErrors(), err)}
if e, ok := err.(errorsInterface); ok {
err = Errors{errors: append(s.GetErrors(), e.GetErrors()...)}
} else {
err = Errors{errors: append(s.GetErrors(), err)}
}
}
s.Error = err
@ -526,10 +530,11 @@ func (s *DB) AddError(err error) error {
return err
}
func (s *DB) GetErrors() []error {
if errs, ok := s.Error.(Errors); ok {
return errs.errors
} else {
func (s *DB) GetErrors() (errors []error) {
if errs, ok := s.Error.(errorsInterface); ok {
return errs.GetErrors()
} else if s.Error != nil {
return []error{s.Error}
}
return
}