From 81c00fdc8fcab379c0c7741bb59b7b8df5535dc7 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 18 Aug 2015 11:06:10 +0800 Subject: [PATCH] Don't add duplicated error --- errors.go | 13 ++++++++++++- main.go | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/errors.go b/errors.go index 23fedd7f..9dfcd2e9 100644 --- a/errors.go +++ b/errors.go @@ -26,7 +26,18 @@ func (errs Errors) GetErrors() []error { } func (errs *Errors) Add(err error) { - errs.errors = append(errs.errors, err) + if errors, ok := err.(errorsInterface); ok { + for _, err := range errors.GetErrors() { + errs.Add(err) + } + } else { + for _, e := range errs.errors { + if err == e { + return + } + } + errs.errors = append(errs.errors, err) + } } func (errs Errors) Error() string { diff --git a/main.go b/main.go index d1898142..50aeb49c 100644 --- a/main.go +++ b/main.go @@ -518,10 +518,10 @@ func (s *DB) AddError(err error) error { s.log(err) } - if e, ok := err.(errorsInterface); ok { - err = Errors{errors: append(s.GetErrors(), e.GetErrors()...)} - } else { - err = Errors{errors: append(s.GetErrors(), err)} + errors := Errors{errors: s.GetErrors()} + errors.Add(err) + if len(errors.GetErrors()) > 1 { + err = errors } }