@@ -60,7 +60,9 @@ func Newf(msg string, args ...interface{}) error {
60
60
// Wrapf an error with format string
61
61
func Wrapf (err error , msg string , args ... interface {}) error {
62
62
wrappedError := errors .Wrapf (err , msg , args ... )
63
- if customErr , ok := err .(customError ); ok {
63
+
64
+ var customErr customError
65
+ if errors .As (err , & customErr ) {
64
66
return customError {
65
67
code : customErr .code ,
66
68
originalError : wrappedError ,
@@ -77,7 +79,8 @@ func Wrap(err error, msg string) error {
77
79
78
80
// WithDisplayMessage returns a error containing a display message
79
81
func WithDisplayMessage (err error , msg string ) error {
80
- if customErr , ok := err .(customError ); ok {
82
+ var customErr customError
83
+ if errors .As (err , & customErr ) {
81
84
return customError {
82
85
code : customErr .code ,
83
86
originalError : err ,
@@ -90,7 +93,8 @@ func WithDisplayMessage(err error, msg string) error {
90
93
91
94
// Code retrives the error code from an error, defaults to InternalError
92
95
func Code (err error ) ErrorCode {
93
- if customErr , ok := err .(customError ); ok {
96
+ var customErr customError
97
+ if errors .As (err , & customErr ) {
94
98
return customErr .code
95
99
}
96
100
@@ -100,22 +104,22 @@ func Code(err error) ErrorCode {
100
104
// Cause retrives the original error
101
105
// Note that it will return the error created internally from github.com/pkg/errors
102
106
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 ))
106
110
}
107
111
108
112
return errors .Cause (err )
109
113
}
110
114
111
115
// DisplayMessage retrives the display message
112
116
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
117
121
}
118
- return string (custom .code )
122
+ return string (customErr .code )
119
123
}
120
124
121
125
return string (InternalError )
0 commit comments