diff --git a/errors/context_map.go b/errors/context_map.go index 71035b38..9c6c2444 100644 --- a/errors/context_map.go +++ b/errors/context_map.go @@ -5,7 +5,7 @@ import ( "fmt" "io" - "github.com/mailgun/holster/stack" + "github.com/mailgun/holster/v3/stack" pkg "github.com/pkg/errors" ) diff --git a/errors/errors.go b/errors/errors.go index d59959dc..6b2fb560 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -95,11 +95,21 @@ import ( "fmt" "io" - "github.com/mailgun/holster/stack" + "github.com/mailgun/holster/v3/stack" pkg "github.com/pkg/errors" "github.com/sirupsen/logrus" ) +// New returns an error with the supplied message. +// New also records the stack trace at the depth specified. +func NewWithDepth(message string, depth int) error { + return &fundamental{ + msg: message, + Stack: stack.New(depth), + } +} + + // New returns an error with the supplied message. // New also records the stack trace at the point it was called. func New(message string) error { @@ -184,6 +194,23 @@ func (w *withStack) Format(s fmt.State, verb rune) { } } +// WrapStack returns an error annotating err with a stack trace +// at the depth indicated. A calling depth of 1 is the function that +// called WrapStack. If err is nil, Wrap returns nil. +func WrapWithDepth(err error, message string, depth int) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: message, + } + return &withStack{ + err, + stack.New(depth), + } +} + // Wrap returns an error annotating err with a stack trace // at the point Wrap is called, and the supplied message. // If err is nil, Wrap returns nil. diff --git a/errors/with_context.go b/errors/with_context.go index 1a2a153b..8c2332ba 100644 --- a/errors/with_context.go +++ b/errors/with_context.go @@ -3,7 +3,7 @@ package errors import ( "fmt" - "github.com/mailgun/holster/stack" + "github.com/mailgun/holster/v3/stack" ) // Implement this interface to pass along unstructured context to the logger diff --git a/errors/with_context_test.go b/errors/with_context_test.go index daff6f5e..8d0b16d1 100644 --- a/errors/with_context_test.go +++ b/errors/with_context_test.go @@ -6,10 +6,10 @@ import ( "strings" "testing" - "github.com/mailgun/holster/errors" - "github.com/mailgun/holster/stack" - . "gopkg.in/check.v1" "github.com/ahmetb/go-linq" + "github.com/mailgun/holster/v3/errors" + "github.com/mailgun/holster/v3/stack" + . "gopkg.in/check.v1" ) type TestError struct {