1 package emperror 2 3 import "github.com/pkg/errors" 4 5 // WrapWith returns an error annotating err with a stack trace 6 // at the point Wrap is called (if there is none attached to the error yet), the supplied message, 7 // and the supplied context. 8 // If err is nil, Wrap returns nil. 9 // 10 // Note: do not use this method when passing errors between goroutines. 11 func WrapWith(err error, message string, keyvals ...interface{}) error { 12 if err == nil { 13 return nil 14 } 15 16 _, ok := getStackTracer(err) 17 18 err = errors.WithMessage(err, message) 19 20 // There is no stack trace in the error, so attach it here 21 if !ok { 22 err = &wrappedError{ 23 err: err, 24 stack: callers(), 25 } 26 } 27 28 // Attach context to the error 29 if len(keyvals) > 0 { 30 err = With(err, keyvals...) 31 } 32 33 return err 34 } 35