Skip to content

Commit 31c154f

Browse files
Merge pull request #25 from nrfta/chore/remove-type-assertion
fix: error type assertion with errors.As
2 parents 2dae9e2 + 4d4504c commit 31c154f

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

errors.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func Newf(msg string, args ...interface{}) error {
6060
// Wrapf an error with format string
6161
func Wrapf(err error, msg string, args ...interface{}) error {
6262
wrappedError := errors.Wrapf(err, msg, args...)
63-
if customErr, ok := err.(customError); ok {
63+
64+
var customErr customError
65+
if errors.As(err, &customErr) {
6466
return customError{
6567
code: customErr.code,
6668
originalError: wrappedError,
@@ -77,7 +79,8 @@ func Wrap(err error, msg string) error {
7779

7880
// WithDisplayMessage returns a error containing a display message
7981
func WithDisplayMessage(err error, msg string) error {
80-
if customErr, ok := err.(customError); ok {
82+
var customErr customError
83+
if errors.As(err, &customErr) {
8184
return customError{
8285
code: customErr.code,
8386
originalError: err,
@@ -90,7 +93,8 @@ func WithDisplayMessage(err error, msg string) error {
9093

9194
// Code retrives the error code from an error, defaults to InternalError
9295
func Code(err error) ErrorCode {
93-
if customErr, ok := err.(customError); ok {
96+
var customErr customError
97+
if errors.As(err, &customErr) {
9498
return customErr.code
9599
}
96100

@@ -100,22 +104,22 @@ func Code(err error) ErrorCode {
100104
// Cause retrives the original error
101105
// Note that it will return the error created internally from github.com/pkg/errors
102106
func Cause(err error) error {
103-
custom, ok := err.(customError)
104-
if ok {
105-
return Cause(errors.Cause(custom.originalError))
107+
var customErr customError
108+
if errors.As(err, &customErr) {
109+
return Cause(errors.Cause(customErr.originalError))
106110
}
107111

108112
return errors.Cause(err)
109113
}
110114

111115
// DisplayMessage retrives the display message
112116
func DisplayMessage(err error) string {
113-
custom, ok := err.(customError)
114-
if ok {
115-
if custom.displayMessage != "" {
116-
return custom.displayMessage
117+
var customErr customError
118+
if errors.As(err, &customErr) {
119+
if customErr.displayMessage != "" {
120+
return customErr.displayMessage
117121
}
118-
return string(custom.code)
122+
return string(customErr.code)
119123
}
120124

121125
return string(InternalError)

errors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,11 @@ var _ = Describe("Errors", func() {
139139

140140
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
141141
})
142+
143+
Describe("#Code", func() {
144+
It("should return a wrapped error code", func() {
145+
err := fmt.Errorf("testing: %w", errors.InvalidArgument.New("invalid argument"))
146+
Expect(errors.Code(err)).To(Equal(errors.InvalidArgument))
147+
})
148+
})
142149
})

0 commit comments

Comments
 (0)