| 1 |
package apierrors |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"io/ioutil" |
| 6 |
"net/http" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
type Code int |
| 11 |
|
| 12 |
const ( |
| 13 |
|
| 14 |
MsgNotLeader = "call sent to a non-leading master" |
| 15 |
|
| 16 |
MsgAuth = "call not authenticated" |
| 17 |
|
| 18 |
MsgUnsubscribed = "no subscription established" |
| 19 |
|
| 20 |
MsgVersion = "incompatible API version" |
| 21 |
|
| 22 |
MsgMalformed = "malformed request" |
| 23 |
|
| 24 |
MsgMediaType = "unsupported media type" |
| 25 |
|
| 26 |
|
| 27 |
MsgRateLimit = "rate limited" |
| 28 |
|
| 29 |
|
| 30 |
MsgUnavailable = "mesos server unavailable" |
| 31 |
|
| 32 |
MsgNotFound = "mesos http endpoint not found" |
| 33 |
|
| 34 |
CodeNotLeader = Code(http.StatusTemporaryRedirect) |
| 35 |
CodeNotAuthenticated = Code(http.StatusUnauthorized) |
| 36 |
CodeUnsubscribed = Code(http.StatusForbidden) |
| 37 |
CodeIncompatibleVersion = Code(http.StatusConflict) |
| 38 |
CodeMalformedRequest = Code(http.StatusBadRequest) |
| 39 |
CodeUnsupportedMediaType = Code(http.StatusNotAcceptable) |
| 40 |
CodeRateLimitExceeded = Code(http.StatusTooManyRequests) |
| 41 |
CodeMesosUnavailable = Code(http.StatusServiceUnavailable) |
| 42 |
CodeNotFound = Code(http.StatusNotFound) |
| 43 |
|
| 44 |
MaxSizeDetails = 4 * 1024 |
| 45 |
) |
| 46 |
|
| 47 |
var ( |
| 48 |
|
| 49 |
ErrorTable = map[Code]string{ |
| 50 |
CodeNotLeader: MsgNotLeader, |
| 51 |
CodeMalformedRequest: MsgMalformed, |
| 52 |
CodeIncompatibleVersion: MsgVersion, |
| 53 |
CodeUnsubscribed: MsgUnsubscribed, |
| 54 |
CodeNotAuthenticated: MsgAuth, |
| 55 |
CodeUnsupportedMediaType: MsgMediaType, |
| 56 |
CodeNotFound: MsgNotFound, |
| 57 |
CodeMesosUnavailable: MsgUnavailable, |
| 58 |
CodeRateLimitExceeded: MsgRateLimit, |
| 59 |
} |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
type Error struct { |
| 64 |
code Code |
| 65 |
message string |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (code Code) IsError() bool { |
| 70 |
return code >= 300 |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func FromResponse(res *http.Response) error { |
| 78 |
if res == nil { |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
code := Code(res.StatusCode) |
| 83 |
if !code.IsError() { |
| 84 |
|
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
var details string |
| 89 |
|
| 90 |
if res.Body != nil { |
| 91 |
defer res.Body.Close() |
| 92 |
buf, _ := ioutil.ReadAll(io.LimitReader(res.Body, MaxSizeDetails)) |
| 93 |
details = string(buf) |
| 94 |
} |
| 95 |
|
| 96 |
return code.Error(details) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func (code Code) Error(details string) error { |
| 101 |
if !code.IsError() { |
| 102 |
return nil |
| 103 |
} |
| 104 |
err := &Error{ |
| 105 |
code: code, |
| 106 |
message: ErrorTable[code], |
| 107 |
} |
| 108 |
if details != "" { |
| 109 |
err.message = err.message + ": " + details |
| 110 |
} |
| 111 |
return err |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
func (e *Error) Error() string { return e.message } |
| 116 |
|
| 117 |
|
| 118 |
func (e *Error) Temporary() bool { |
| 119 |
switch e.code { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
case CodeRateLimitExceeded, CodeMesosUnavailable: |
| 126 |
return true |
| 127 |
default: |
| 128 |
return false |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
var CodesIndicatingSubscriptionLoss = func(codes ...Code) map[Code]struct{} { |
| 137 |
result := make(map[Code]struct{}, len(codes)) |
| 138 |
for _, code := range codes { |
| 139 |
result[code] = struct{}{} |
| 140 |
} |
| 141 |
return result |
| 142 |
}( |
| 143 |
|
| 144 |
CodeUnsubscribed, |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func (e *Error) SubscriptionLoss() (result bool) { |
| 150 |
_, result = CodesIndicatingSubscriptionLoss[e.code] |
| 151 |
return |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func (code Code) Matches(err error) bool { |
| 156 |
if err == nil { |
| 157 |
return !code.IsError() |
| 158 |
} |
| 159 |
apiErr, ok := err.(*Error) |
| 160 |
return ok && apiErr.code == code |
| 161 |
} |