Revert "Remove WithStack and WithMessage public functions"
This reverts commit 1b876e063eebebbcbab83aafa8bc631edef98fff.
diff --git a/errors.go b/errors.go
index 1c9731a..960d7c7 100644
--- a/errors.go
+++ b/errors.go
@@ -134,6 +134,18 @@
}
}
+// WithStack annotates err with a stack trace at the point WithStack was called.
+// If err is nil, WithStack returns nil.
+func WithStack(err error) error {
+ if err == nil {
+ return nil
+ }
+ return &withStack{
+ err,
+ callers(),
+ }
+}
+
type withStack struct {
error
*stack
@@ -189,6 +201,18 @@
}
}
+// WithMessage annotates err with a new message.
+// If err is nil, WithStack returns nil.
+func WithMessage(err error, message string) error {
+ if err == nil {
+ return nil
+ }
+ return &withMessage{
+ cause: err,
+ msg: message,
+ }
+}
+
type withMessage struct {
cause error
msg string
diff --git a/stack_test.go b/stack_test.go
index 915d11f..2624e45 100644
--- a/stack_test.go
+++ b/stack_test.go
@@ -155,12 +155,12 @@
"github.com/pkg/errors/stack_test.go",
}}
- for i, tt := range tests {
+ for _, tt := range tests {
pc := tt.Frame.pc()
fn := runtime.FuncForPC(pc)
file, _ := fn.FileLine(pc)
got := trimGOPATH(fn.Name(), file)
- testFormatRegexp(t, i, got, "%s", tt.want)
+ testFormatRegexp(t, got, "%s", tt.want)
}
}