Skip to content

Commit a5c2b0c

Browse files
committed
Add more error handling helpers
1 parent c38f55d commit a5c2b0c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

errors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ const (
104104
UnsupportedVersion ErrorCode = "unsupported_version"
105105
)
106106

107+
// IsErrorCode is a concenience method to check if an error is a specific error code from Duffel.
108+
// This simplifies error handling branches without needing to type cast multiple times in your code.
109+
func IsErrorCode(err error, code ErrorCode) bool {
110+
if err, ok := err.(*DuffelError); ok {
111+
return err.IsCode(code)
112+
}
113+
return false
114+
}
115+
116+
// IsErrorType is a concenience method to check if an error is a specific error type from Duffel.
117+
// This simplifies error handling branches without needing to type cast multiple times in your code.
118+
func IsErrorType(err error, typ ErrorType) bool {
119+
if err, ok := err.(*DuffelError); ok {
120+
return err.IsType(typ)
121+
}
122+
return false
123+
}
124+
107125
type DuffelError struct {
108126
Meta ErrorMeta `json:"meta"`
109127
Errors []Error `json:"errors"`

0 commit comments

Comments
 (0)