-
Notifications
You must be signed in to change notification settings - Fork 38
/
unwrap.go
46 lines (40 loc) · 895 Bytes
/
unwrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package apierr
func inheritErr(err error, message string) *wrapError {
return &wrapError{
wrap: err,
message: message,
}
}
type wrapError struct {
wrap error
message string
}
func (e *wrapError) Error() string {
return e.message
}
func (e *wrapError) Unwrap() error {
return e.wrap
}
func ByStatusCode(statusCode int) (error, bool) {
err, ok := statusCodeMapping[statusCode]
return err, ok
}
// Unwrap error for easier client code checking
//
// See https://pkg.go.dev/errors#example-Unwrap
func (apiError *APIError) Unwrap() error {
if apiError.unwrap != nil {
return apiError.unwrap
}
byErrorCode, ok := errorCodeMapping[apiError.ErrorCode]
if ok {
return byErrorCode
}
byStatusCode, ok := ByStatusCode(apiError.StatusCode)
if ok {
return byStatusCode
}
// A nil error returned from e.Unwrap() indicates that e does not wrap
// any error.
return nil
}