Skip to content

Commit

Permalink
added WithStackInfo.AttachGenerals(errs ...interface{}) to simplify t…
Browse files Browse the repository at this point in the history
…he user-side defer-recover codes
  • Loading branch information
hedzr committed Feb 16, 2022
1 parent 9671744 commit f4082b1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func (w *WithStackInfo) Unwrap() error {
}

// Attach appends errs
// WithStackInfo.Attach() can only wrap and hold one child error object.
func (w *WithStackInfo) Attach(errs ...error) *WithStackInfo {
if w.error == nil {
if len(errs) > 1 {
Expand All @@ -428,6 +429,28 @@ func (w *WithStackInfo) Attach(errs ...error) *WithStackInfo {
return w
}

// AttachGenerals appends errs if the general object is a error object
// WithStackInfo.AttachGenerals() can only wrap and hold one child error object.
func (w *WithStackInfo) AttachGenerals(errs ...interface{}) *WithStackInfo {
if w.error == nil {
if len(errs) > 1 {
panic("*WithStackInfo.AttachGenerals() can only wrap one child error object.")
}
for _, e := range errs {
if e1, ok := e.(error); ok {
w.error = e1
}
}
return w
}

if x, ok := w.error.(interface{ AttachGenerals(errs ...interface{}) }); ok {
x.AttachGenerals(errs...)
}

return w
}

// IsEmpty tests has attached errors
func (w *WithStackInfo) IsEmpty() bool {
if x, ok := w.error.(interface{ IsEmpty() bool }); ok {
Expand Down

0 comments on commit f4082b1

Please sign in to comment.