Debian Codegraph @

api/v1/lib/httpcli/apierrors/apierrors.go

raw symbols SCIP
1 package apierrors
2
3 import (
4 "io"
5 "io/ioutil"
6 "net/http"
7 )
8
9 // Code is a Mesos HTTP v1 API response status code
10 type Code int
11
12 const (
13 // MsgNotLeader is returned by Do calls that are sent to a non leading Mesos master.
14 MsgNotLeader = "call sent to a non-leading master"
15 // MsgAuth is returned by Do calls that are not successfully authenticated.
16 MsgAuth = "call not authenticated"
17 // MsgUnsubscribed is returned by Do calls that are sent before a subscription is established.
18 MsgUnsubscribed = "no subscription established"
19 // MsgVersion is returned by Do calls that are sent to an incompatible API version.
20 MsgVersion = "incompatible API version"
21 // MsgMalformed is returned by Do calls that are malformed.
22 MsgMalformed = "malformed request"
23 // MsgMediaType is returned by Do calls that are sent with an unsupported media type.
24 MsgMediaType = "unsupported media type"
25 // MsgRateLimit is returned by Do calls that are rate limited. This is a temporary condition
26 // that should clear.
27 MsgRateLimit = "rate limited"
28 // MsgUnavailable is returned by Do calls that are sent to a master or agent that's in recovery, or
29 // does not yet realize that it's the leader. This is a temporary condition that should clear.
30 MsgUnavailable = "mesos server unavailable"
31 // MsgNotFound could happen if the master or agent libprocess has not yet set up http routes.
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 // MaxSizeDetails limits the length of the details message read from a response body
45 )
46
47 var (
48 // ErrorTable maps HTTP response codes to their respective Mesos v1 API error messages.
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 // Error captures HTTP v1 API error codes and messages generated by Mesos.
63 type Error struct {
64 code Code // code is the HTTP response status code generated by Mesos
65 message string // message briefly summarizes the nature of the error, possibly includes details from Mesos
66 }
67
68 // IsError returns true for all HTTP status codes that are not considered informational or successful.
69 func (code Code) IsError() bool {
70 return code >= 300
71 }
72
73 // FromResponse returns an `*Error` for a response containing a status code that indicates an error condition.
74 // The response body (if any) is captured in the Error.Details field.
75 // Returns nil for nil responses and responses with non-error status codes.
76 // See IsErrorCode.
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 // non-error HTTP response codes don't generate errors
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 // Error generates an error from the given status code and detail string.
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 // Error implements error interface
115 func (e *Error) Error() string { return e.message }
116
117 // Temporary returns true if the error is a temporary condition that should eventually clear.
118 func (e *Error) Temporary() bool {
119 switch e.code {
120 // TODO(jdef): NotFound **could** be a temporary error because there's a race at mesos startup in which the
121 // HTTP server responds before the internal listeners have been initialized. But it could also be reported
122 // because the client is accessing an invalid endpoint; as of right now, a client cannot distinguish between
123 // these cases.
124 // https://issues.apache.org/jira/browse/MESOS-7697
125 case CodeRateLimitExceeded, CodeMesosUnavailable:
126 return true
127 default:
128 return false
129 }
130 }
131
132 // CodesIndicatingSubscriptionLoss is a set of apierror.Code entries which each indicate that
133 // the event subscription stream has been severed between the scheduler and mesos. It's respresented
134 // as a public map variable so that clients can program additional error codes (if such are discovered)
135 // without hacking the code of the mesos-go library directly.
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 // expand this list as we discover other errors that guarantee we've lost our event subscription.
144 CodeUnsubscribed,
145 )
146
147 // SubscriptionLoss returns true if the error indicates that the event subscription stream has been severed
148 // between mesos and a mesos client.
149 func (e *Error) SubscriptionLoss() (result bool) {
150 _, result = CodesIndicatingSubscriptionLoss[e.code]
151 return
152 }
153
154 // Matches returns true if the given error is an API error with a matching error code
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 }