From d2e8955a4473205423e20dd8ec7f56244b0786f6 Mon Sep 17 00:00:00 2001 From: "Derrick J. Wippler" Date: Tue, 21 Apr 2020 12:43:59 -0500 Subject: [PATCH] Added NewWithDepth() WrapWithDepth() --- errors/context_map.go | 2 +- errors/errors.go | 29 ++++++++++++++++++++++++++++- errors/with_context.go | 2 +- errors/with_context_test.go | 6 +++--- 4 files changed, 33 insertions(+), 6 deletions(-) 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 {