Skip to content

Commit

Permalink
feat: remove pkg/errors for stdlib errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeyheath committed Mar 30, 2024
1 parent 1af8ec4 commit 2595678
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
30 changes: 8 additions & 22 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package errors

import (
"errors"
"fmt"

"github.com/pkg/errors"
)

/*
Expand All @@ -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
}
Expand Down
24 changes: 22 additions & 2 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand All @@ -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")
Expand Down Expand Up @@ -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())
}
7 changes: 2 additions & 5 deletions errors/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 4 additions & 6 deletions errors/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 2595678

Please sign in to comment.