Skip to content

Commit

Permalink
Added method Num to errutil which returns number of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jan 19, 2016
1 parent c1c0a91 commit 546900a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions errutil/errutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package errutil

// Errors is struct for handling many errors at once
type Errors struct {
num int
errors []error
}

Expand All @@ -24,13 +25,14 @@ func NewErrors() *Errors {

// Add adds new error to slice
func (e *Errors) Add(errs ...error) *Errors {
if errs == nil || len(errs) == 0 {
if errs == nil {
return e
}

for _, err := range errs {
if err != nil {
e.errors = append(e.errors, err)
e.num++
}
}

Expand All @@ -39,11 +41,11 @@ func (e *Errors) Add(errs ...error) *Errors {

// Last return last error in slice
func (e *Errors) Last() error {
if e.errors == nil || len(e.errors) == 0 {
if e.errors == nil || e.num == 0 {
return nil
}

return e.errors[len(e.errors)-1]
return e.errors[e.num-1]
}

// All return all errors in slice
Expand All @@ -61,5 +63,10 @@ func (e *Errors) HasErrors() bool {
return false
}

return len(e.errors) != 0
return e.num != 0
}

// Num return number of errors
func (e *Errors) Num() int {
return e.num
}
1 change: 1 addition & 0 deletions errutil/errutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *ErrSuite) TestPositive(c *C) {
errs.Add(errors.New("4"))
errs.Add(errors.New("5"))

c.Assert(errs.Num(), Equals, 5)
c.Assert(errs.All(), HasLen, 5)
c.Assert(errs.HasErrors(), Equals, true)
c.Assert(errs.Last(), DeepEquals, errors.New("5"))
Expand Down

0 comments on commit 546900a

Please sign in to comment.