Skip to content

Commit

Permalink
fix: WithErrors will check IsEmpty on an error container and avoid ad…
Browse files Browse the repository at this point in the history
…ding it if empty.
  • Loading branch information
hedzr committed Feb 25, 2022
1 parent 6aa6239 commit da3db1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 12 additions & 1 deletion causes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,23 @@ func (w *causes2) Defer(err *error) {
}

// WithErrors appends errs
//
// WithStackInfo.Attach() can only wrap and hold one child error object.
//
// WithErrors attach child errors into an error container.
// For a container which has IsEmpty() interface, it would not be attach if it is empty (i.e. no errors).
// For a nil error object, it will be ignored.
func (w *causes2) WithErrors(errs ...error) *causes2 {
if len(errs) > 0 {
for _, e := range errs {
if e != nil {
w.Causers = append(w.Causers, e)
if check, ok := e.(interface{ IsEmpty() bool }); ok {
if check.IsEmpty() == false {
w.Causers = append(w.Causers, e)
}
} else {
w.Causers = append(w.Causers, e)
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions def.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Buildable interface {
WithCode(code Code) Buildable
// WithErrors attaches the given errs as inner errors.
// WithErrors is like our old Attach().
//
// It wraps the inner errors into underlying container and
// represents them all in a singular up-level error object.
// The wrapped inner errors can be retrieved with errors.Causes:
Expand All @@ -75,6 +76,9 @@ type Buildable interface {
// e = errors.Unwrap(err)
// }
//
// WithErrors attach child errors into an error container.
// For a container which has IsEmpty() interface, it would not be attach if it is empty (i.e. no errors).
// For a nil error object, it will be ignored.
WithErrors(errs ...error) Buildable
// WithData appends errs if the general object is a error object.
// It can be used in defer-recover block typically. For example:
Expand Down
8 changes: 7 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func New(args ...interface{}) Error {
// Opt _
type Opt func(s *builder)

// WithErrors _
// WithErrors attach child errors into an error container.
// For a container which has IsEmpty() interface, it would not be attach if it is empty (i.e. no errors).
// For a nil error object, it will be ignored.
func WithErrors(errs ...error) Opt {
return func(s *builder) {
s.WithErrors(errs...)
Expand Down Expand Up @@ -70,6 +72,8 @@ type Builder interface {
// WithSkip specifies a special number of stack frames that will be ignored.
WithSkip(skip int) Builder
// WithErrors attaches the given errs as inner errors.
// For a container which has IsEmpty() interface, it would not be attach if it is empty (i.e. no errors).
// For a nil error object, it will be ignored.
WithErrors(errs ...error) Builder
// WithMessage formats the error message
WithMessage(message string, args ...interface{}) Builder
Expand Down Expand Up @@ -119,6 +123,8 @@ func (s *builder) WithMessage(message string, args ...interface{}) Builder {
}

// WithErrors attaches the given errs as inner errors.
// For a container which has IsEmpty() interface, it would not be attach if it is empty (i.e. no errors).
// For a nil error object, it will be ignored.
func (s *builder) WithErrors(errs ...error) Builder {
_ = s.causes2.WithErrors(errs...)
return s
Expand Down

0 comments on commit da3db1b

Please sign in to comment.