-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
66 lines (54 loc) · 1.38 KB
/
errors.go
File metadata and controls
66 lines (54 loc) · 1.38 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package client
import (
"errors"
"fmt"
"net/http"
)
// StatusCodeError is a common interface implemented by Error and the SDK's GenericOpenAPIError.
type StatusCodeError interface {
error
GetStatusCode() int
}
// Error is an error returned by SDK helper functions in this package.
type Error struct {
Message string
Resource string
Name string
StatusCode int
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) GetStatusCode() int {
return e.StatusCode
}
func NewNotFoundError(resource, name string) *Error {
return &Error{
Message: fmt.Sprintf("%s %q not found", resource, name),
Resource: resource,
Name: name,
StatusCode: http.StatusNotFound,
}
}
// GetStatusCode returns the attached error code if the given error implements StatusCodeError or 0 otherwise.
func GetStatusCode(err error) int {
var statusCodeError StatusCodeError
if ok := errors.As(err, &statusCodeError); !ok {
return 0
}
return statusCodeError.GetStatusCode()
}
func IsNotFound(err error) bool {
return GetStatusCode(err) == http.StatusNotFound
}
func IsConflict(err error) bool { return GetStatusCode(err) == http.StatusConflict }
// IgnoreNotFoundError ignore not found error
func IgnoreNotFoundError(err error) error {
if IsNotFound(err) {
return nil
}
return err
}
func IsConflictError(err error) bool {
return GetStatusCode(err) == http.StatusConflict
}