diff --git a/errors/errors.go b/errors/errors.go index d7c2b2bb..294e2ea8 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -1,9 +1,8 @@ package errors import ( + "errors" "fmt" - - "github.com/pkg/errors" ) /* @@ -12,38 +11,25 @@ import ( / Only the public parts of errors will be returned to the caller, the rest can be logged server side. */ -// hoist some error functions for convenience -var ( - Wrap = errors.Wrap - Wrapf = errors.Wrapf - New = errors.New - Errorf = errors.Errorf -) - -type public interface { - GetPublicMessage() string -} - -type internal interface { - GetInternalError() error -} - func optionalString(s string) *string { return &s } func GetPublicMessage(err error) *string { - pu, ok := errors.Cause(err).(public) + pubErr := &publicError{} + ok := errors.As(err, &pubErr) + if ok { - return optionalString(pu.GetPublicMessage()) + return optionalString(pubErr.GetPublicMessage()) } return nil } func GetInternalError(err error) error { - in, ok := errors.Cause(err).(internal) + pubErr := &publicError{} + ok := errors.As(err, &pubErr) if ok { - return in.GetInternalError() + return pubErr.GetInternalError() } return nil } diff --git a/errors/errors_test.go b/errors/errors_test.go index 589922b3..79cb372a 100644 --- a/errors/errors_test.go +++ b/errors/errors_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/require" ) @@ -26,7 +25,6 @@ func TestPublicErrors(t *testing.T) { baseErr := GetInternalError(err) r.NotNil(baseErr) r.Equal("base error", baseErr.Error()) - r.Equal("base error", errors.Cause(baseErr).Error()) // not a publicError err = fmt.Errorf("scary error") @@ -63,3 +61,25 @@ func TestGetInternalErrorNilError(t *testing.T) { r.NoError(nilError.GetInternalError()) } + +func TestChainOfErrors(t *testing.T) { + r := require.New(t) + + pubErr1 := PublicWrap(fmt.Errorf("error1"), "public message1") + err2 := fmt.Errorf("error2") + err3 := fmt.Errorf("error3") + pubErr2 := PublicWrap(fmt.Errorf("error4"), "public message2") + err5 := fmt.Errorf("error5") + + //err5 -> pubErr2 -> err3 -> err2 -> pubErr1 + err := fmt.Errorf("%w: %w: %w: %w: %w", err5, pubErr2, err3, err2, pubErr1) + msg := GetPublicMessage(err) + r.NotNil(msg) + // gets the first public message off the chain + r.Equal("public message2", *msg) + + // gets the first internal error off the chain + intErr := GetInternalError(err) + r.NotNil(intErr) + r.Equal("error4", intErr.Error()) +} diff --git a/errors/go.mod b/errors/go.mod index 3596578a..819b44f2 100644 --- a/errors/go.mod +++ b/errors/go.mod @@ -4,16 +4,13 @@ go 1.21 toolchain go1.21.1 -require ( - github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.8.4 -) +require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.10.0 // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/errors/go.sum b/errors/go.sum index 7751f73f..a1dc9ac9 100644 --- a/errors/go.sum +++ b/errors/go.sum @@ -9,15 +9,13 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= -github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=