errors.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package errors
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net/http"
  18. "reflect"
  19. "strings"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/runtime/schema"
  23. "k8s.io/apimachinery/pkg/util/validation/field"
  24. )
  25. const (
  26. // StatusTooManyRequests means the server experienced too many requests within a
  27. // given window and that the client must wait to perform the action again.
  28. StatusTooManyRequests = 429
  29. )
  30. // StatusError is an error intended for consumption by a REST API server; it can also be
  31. // reconstructed by clients from a REST response. Public to allow easy type switches.
  32. type StatusError struct {
  33. ErrStatus metav1.Status
  34. }
  35. // APIStatus is exposed by errors that can be converted to an api.Status object
  36. // for finer grained details.
  37. type APIStatus interface {
  38. Status() metav1.Status
  39. }
  40. var _ error = &StatusError{}
  41. // Error implements the Error interface.
  42. func (e *StatusError) Error() string {
  43. return e.ErrStatus.Message
  44. }
  45. // Status allows access to e's status without having to know the detailed workings
  46. // of StatusError.
  47. func (e *StatusError) Status() metav1.Status {
  48. return e.ErrStatus
  49. }
  50. // DebugError reports extended info about the error to debug output.
  51. func (e *StatusError) DebugError() (string, []interface{}) {
  52. if out, err := json.MarshalIndent(e.ErrStatus, "", " "); err == nil {
  53. return "server response object: %s", []interface{}{string(out)}
  54. }
  55. return "server response object: %#v", []interface{}{e.ErrStatus}
  56. }
  57. // UnexpectedObjectError can be returned by FromObject if it's passed a non-status object.
  58. type UnexpectedObjectError struct {
  59. Object runtime.Object
  60. }
  61. // Error returns an error message describing 'u'.
  62. func (u *UnexpectedObjectError) Error() string {
  63. return fmt.Sprintf("unexpected object: %v", u.Object)
  64. }
  65. // FromObject generates an StatusError from an metav1.Status, if that is the type of obj; otherwise,
  66. // returns an UnexpecteObjectError.
  67. func FromObject(obj runtime.Object) error {
  68. switch t := obj.(type) {
  69. case *metav1.Status:
  70. return &StatusError{ErrStatus: *t}
  71. case runtime.Unstructured:
  72. var status metav1.Status
  73. obj := t.UnstructuredContent()
  74. if !reflect.DeepEqual(obj["kind"], "Status") {
  75. break
  76. }
  77. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(t.UnstructuredContent(), &status); err != nil {
  78. return err
  79. }
  80. if status.APIVersion != "v1" && status.APIVersion != "meta.k8s.io/v1" {
  81. break
  82. }
  83. return &StatusError{ErrStatus: status}
  84. }
  85. return &UnexpectedObjectError{obj}
  86. }
  87. // NewNotFound returns a new error which indicates that the resource of the kind and the name was not found.
  88. func NewNotFound(qualifiedResource schema.GroupResource, name string) *StatusError {
  89. return &StatusError{metav1.Status{
  90. Status: metav1.StatusFailure,
  91. Code: http.StatusNotFound,
  92. Reason: metav1.StatusReasonNotFound,
  93. Details: &metav1.StatusDetails{
  94. Group: qualifiedResource.Group,
  95. Kind: qualifiedResource.Resource,
  96. Name: name,
  97. },
  98. Message: fmt.Sprintf("%s %q not found", qualifiedResource.String(), name),
  99. }}
  100. }
  101. // NewAlreadyExists returns an error indicating the item requested exists by that identifier.
  102. func NewAlreadyExists(qualifiedResource schema.GroupResource, name string) *StatusError {
  103. return &StatusError{metav1.Status{
  104. Status: metav1.StatusFailure,
  105. Code: http.StatusConflict,
  106. Reason: metav1.StatusReasonAlreadyExists,
  107. Details: &metav1.StatusDetails{
  108. Group: qualifiedResource.Group,
  109. Kind: qualifiedResource.Resource,
  110. Name: name,
  111. },
  112. Message: fmt.Sprintf("%s %q already exists", qualifiedResource.String(), name),
  113. }}
  114. }
  115. // NewUnauthorized returns an error indicating the client is not authorized to perform the requested
  116. // action.
  117. func NewUnauthorized(reason string) *StatusError {
  118. message := reason
  119. if len(message) == 0 {
  120. message = "not authorized"
  121. }
  122. return &StatusError{metav1.Status{
  123. Status: metav1.StatusFailure,
  124. Code: http.StatusUnauthorized,
  125. Reason: metav1.StatusReasonUnauthorized,
  126. Message: message,
  127. }}
  128. }
  129. // NewForbidden returns an error indicating the requested action was forbidden
  130. func NewForbidden(qualifiedResource schema.GroupResource, name string, err error) *StatusError {
  131. var message string
  132. if qualifiedResource.Empty() {
  133. message = fmt.Sprintf("forbidden: %v", err)
  134. } else if name == "" {
  135. message = fmt.Sprintf("%s is forbidden: %v", qualifiedResource.String(), err)
  136. } else {
  137. message = fmt.Sprintf("%s %q is forbidden: %v", qualifiedResource.String(), name, err)
  138. }
  139. return &StatusError{metav1.Status{
  140. Status: metav1.StatusFailure,
  141. Code: http.StatusForbidden,
  142. Reason: metav1.StatusReasonForbidden,
  143. Details: &metav1.StatusDetails{
  144. Group: qualifiedResource.Group,
  145. Kind: qualifiedResource.Resource,
  146. Name: name,
  147. },
  148. Message: message,
  149. }}
  150. }
  151. // NewConflict returns an error indicating the item can't be updated as provided.
  152. func NewConflict(qualifiedResource schema.GroupResource, name string, err error) *StatusError {
  153. return &StatusError{metav1.Status{
  154. Status: metav1.StatusFailure,
  155. Code: http.StatusConflict,
  156. Reason: metav1.StatusReasonConflict,
  157. Details: &metav1.StatusDetails{
  158. Group: qualifiedResource.Group,
  159. Kind: qualifiedResource.Resource,
  160. Name: name,
  161. },
  162. Message: fmt.Sprintf("Operation cannot be fulfilled on %s %q: %v", qualifiedResource.String(), name, err),
  163. }}
  164. }
  165. // NewApplyConflict returns an error including details on the requests apply conflicts
  166. func NewApplyConflict(causes []metav1.StatusCause, message string) *StatusError {
  167. return &StatusError{ErrStatus: metav1.Status{
  168. Status: metav1.StatusFailure,
  169. Code: http.StatusConflict,
  170. Reason: metav1.StatusReasonConflict,
  171. Details: &metav1.StatusDetails{
  172. // TODO: Get obj details here?
  173. Causes: causes,
  174. },
  175. Message: message,
  176. }}
  177. }
  178. // NewGone returns an error indicating the item no longer available at the server and no forwarding address is known.
  179. func NewGone(message string) *StatusError {
  180. return &StatusError{metav1.Status{
  181. Status: metav1.StatusFailure,
  182. Code: http.StatusGone,
  183. Reason: metav1.StatusReasonGone,
  184. Message: message,
  185. }}
  186. }
  187. // NewResourceExpired creates an error that indicates that the requested resource content has expired from
  188. // the server (usually due to a resourceVersion that is too old).
  189. func NewResourceExpired(message string) *StatusError {
  190. return &StatusError{metav1.Status{
  191. Status: metav1.StatusFailure,
  192. Code: http.StatusGone,
  193. Reason: metav1.StatusReasonExpired,
  194. Message: message,
  195. }}
  196. }
  197. // NewInvalid returns an error indicating the item is invalid and cannot be processed.
  198. func NewInvalid(qualifiedKind schema.GroupKind, name string, errs field.ErrorList) *StatusError {
  199. causes := make([]metav1.StatusCause, 0, len(errs))
  200. for i := range errs {
  201. err := errs[i]
  202. causes = append(causes, metav1.StatusCause{
  203. Type: metav1.CauseType(err.Type),
  204. Message: err.ErrorBody(),
  205. Field: err.Field,
  206. })
  207. }
  208. return &StatusError{metav1.Status{
  209. Status: metav1.StatusFailure,
  210. Code: http.StatusUnprocessableEntity,
  211. Reason: metav1.StatusReasonInvalid,
  212. Details: &metav1.StatusDetails{
  213. Group: qualifiedKind.Group,
  214. Kind: qualifiedKind.Kind,
  215. Name: name,
  216. Causes: causes,
  217. },
  218. Message: fmt.Sprintf("%s %q is invalid: %v", qualifiedKind.String(), name, errs.ToAggregate()),
  219. }}
  220. }
  221. // NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
  222. func NewBadRequest(reason string) *StatusError {
  223. return &StatusError{metav1.Status{
  224. Status: metav1.StatusFailure,
  225. Code: http.StatusBadRequest,
  226. Reason: metav1.StatusReasonBadRequest,
  227. Message: reason,
  228. }}
  229. }
  230. // NewTooManyRequests creates an error that indicates that the client must try again later because
  231. // the specified endpoint is not accepting requests. More specific details should be provided
  232. // if client should know why the failure was limited4.
  233. func NewTooManyRequests(message string, retryAfterSeconds int) *StatusError {
  234. return &StatusError{metav1.Status{
  235. Status: metav1.StatusFailure,
  236. Code: http.StatusTooManyRequests,
  237. Reason: metav1.StatusReasonTooManyRequests,
  238. Message: message,
  239. Details: &metav1.StatusDetails{
  240. RetryAfterSeconds: int32(retryAfterSeconds),
  241. },
  242. }}
  243. }
  244. // NewServiceUnavailable creates an error that indicates that the requested service is unavailable.
  245. func NewServiceUnavailable(reason string) *StatusError {
  246. return &StatusError{metav1.Status{
  247. Status: metav1.StatusFailure,
  248. Code: http.StatusServiceUnavailable,
  249. Reason: metav1.StatusReasonServiceUnavailable,
  250. Message: reason,
  251. }}
  252. }
  253. // NewMethodNotSupported returns an error indicating the requested action is not supported on this kind.
  254. func NewMethodNotSupported(qualifiedResource schema.GroupResource, action string) *StatusError {
  255. return &StatusError{metav1.Status{
  256. Status: metav1.StatusFailure,
  257. Code: http.StatusMethodNotAllowed,
  258. Reason: metav1.StatusReasonMethodNotAllowed,
  259. Details: &metav1.StatusDetails{
  260. Group: qualifiedResource.Group,
  261. Kind: qualifiedResource.Resource,
  262. },
  263. Message: fmt.Sprintf("%s is not supported on resources of kind %q", action, qualifiedResource.String()),
  264. }}
  265. }
  266. // NewServerTimeout returns an error indicating the requested action could not be completed due to a
  267. // transient error, and the client should try again.
  268. func NewServerTimeout(qualifiedResource schema.GroupResource, operation string, retryAfterSeconds int) *StatusError {
  269. return &StatusError{metav1.Status{
  270. Status: metav1.StatusFailure,
  271. Code: http.StatusInternalServerError,
  272. Reason: metav1.StatusReasonServerTimeout,
  273. Details: &metav1.StatusDetails{
  274. Group: qualifiedResource.Group,
  275. Kind: qualifiedResource.Resource,
  276. Name: operation,
  277. RetryAfterSeconds: int32(retryAfterSeconds),
  278. },
  279. Message: fmt.Sprintf("The %s operation against %s could not be completed at this time, please try again.", operation, qualifiedResource.String()),
  280. }}
  281. }
  282. // NewServerTimeoutForKind should not exist. Server timeouts happen when accessing resources, the Kind is just what we
  283. // happened to be looking at when the request failed. This delegates to keep code sane, but we should work towards removing this.
  284. func NewServerTimeoutForKind(qualifiedKind schema.GroupKind, operation string, retryAfterSeconds int) *StatusError {
  285. return NewServerTimeout(schema.GroupResource{Group: qualifiedKind.Group, Resource: qualifiedKind.Kind}, operation, retryAfterSeconds)
  286. }
  287. // NewInternalError returns an error indicating the item is invalid and cannot be processed.
  288. func NewInternalError(err error) *StatusError {
  289. return &StatusError{metav1.Status{
  290. Status: metav1.StatusFailure,
  291. Code: http.StatusInternalServerError,
  292. Reason: metav1.StatusReasonInternalError,
  293. Details: &metav1.StatusDetails{
  294. Causes: []metav1.StatusCause{{Message: err.Error()}},
  295. },
  296. Message: fmt.Sprintf("Internal error occurred: %v", err),
  297. }}
  298. }
  299. // NewTimeoutError returns an error indicating that a timeout occurred before the request
  300. // could be completed. Clients may retry, but the operation may still complete.
  301. func NewTimeoutError(message string, retryAfterSeconds int) *StatusError {
  302. return &StatusError{metav1.Status{
  303. Status: metav1.StatusFailure,
  304. Code: http.StatusGatewayTimeout,
  305. Reason: metav1.StatusReasonTimeout,
  306. Message: fmt.Sprintf("Timeout: %s", message),
  307. Details: &metav1.StatusDetails{
  308. RetryAfterSeconds: int32(retryAfterSeconds),
  309. },
  310. }}
  311. }
  312. // NewTooManyRequestsError returns an error indicating that the request was rejected because
  313. // the server has received too many requests. Client should wait and retry. But if the request
  314. // is perishable, then the client should not retry the request.
  315. func NewTooManyRequestsError(message string) *StatusError {
  316. return &StatusError{metav1.Status{
  317. Status: metav1.StatusFailure,
  318. Code: StatusTooManyRequests,
  319. Reason: metav1.StatusReasonTooManyRequests,
  320. Message: fmt.Sprintf("Too many requests: %s", message),
  321. }}
  322. }
  323. // NewRequestEntityTooLargeError returns an error indicating that the request
  324. // entity was too large.
  325. func NewRequestEntityTooLargeError(message string) *StatusError {
  326. return &StatusError{metav1.Status{
  327. Status: metav1.StatusFailure,
  328. Code: http.StatusRequestEntityTooLarge,
  329. Reason: metav1.StatusReasonRequestEntityTooLarge,
  330. Message: fmt.Sprintf("Request entity too large: %s", message),
  331. }}
  332. }
  333. // NewGenericServerResponse returns a new error for server responses that are not in a recognizable form.
  334. func NewGenericServerResponse(code int, verb string, qualifiedResource schema.GroupResource, name, serverMessage string, retryAfterSeconds int, isUnexpectedResponse bool) *StatusError {
  335. reason := metav1.StatusReasonUnknown
  336. message := fmt.Sprintf("the server responded with the status code %d but did not return more information", code)
  337. switch code {
  338. case http.StatusConflict:
  339. if verb == "POST" {
  340. reason = metav1.StatusReasonAlreadyExists
  341. } else {
  342. reason = metav1.StatusReasonConflict
  343. }
  344. message = "the server reported a conflict"
  345. case http.StatusNotFound:
  346. reason = metav1.StatusReasonNotFound
  347. message = "the server could not find the requested resource"
  348. case http.StatusBadRequest:
  349. reason = metav1.StatusReasonBadRequest
  350. message = "the server rejected our request for an unknown reason"
  351. case http.StatusUnauthorized:
  352. reason = metav1.StatusReasonUnauthorized
  353. message = "the server has asked for the client to provide credentials"
  354. case http.StatusForbidden:
  355. reason = metav1.StatusReasonForbidden
  356. // the server message has details about who is trying to perform what action. Keep its message.
  357. message = serverMessage
  358. case http.StatusNotAcceptable:
  359. reason = metav1.StatusReasonNotAcceptable
  360. // the server message has details about what types are acceptable
  361. message = serverMessage
  362. case http.StatusUnsupportedMediaType:
  363. reason = metav1.StatusReasonUnsupportedMediaType
  364. // the server message has details about what types are acceptable
  365. message = serverMessage
  366. case http.StatusMethodNotAllowed:
  367. reason = metav1.StatusReasonMethodNotAllowed
  368. message = "the server does not allow this method on the requested resource"
  369. case http.StatusUnprocessableEntity:
  370. reason = metav1.StatusReasonInvalid
  371. message = "the server rejected our request due to an error in our request"
  372. case http.StatusServiceUnavailable:
  373. reason = metav1.StatusReasonServiceUnavailable
  374. message = "the server is currently unable to handle the request"
  375. case http.StatusGatewayTimeout:
  376. reason = metav1.StatusReasonTimeout
  377. message = "the server was unable to return a response in the time allotted, but may still be processing the request"
  378. case http.StatusTooManyRequests:
  379. reason = metav1.StatusReasonTooManyRequests
  380. message = "the server has received too many requests and has asked us to try again later"
  381. default:
  382. if code >= 500 {
  383. reason = metav1.StatusReasonInternalError
  384. message = fmt.Sprintf("an error on the server (%q) has prevented the request from succeeding", serverMessage)
  385. }
  386. }
  387. switch {
  388. case !qualifiedResource.Empty() && len(name) > 0:
  389. message = fmt.Sprintf("%s (%s %s %s)", message, strings.ToLower(verb), qualifiedResource.String(), name)
  390. case !qualifiedResource.Empty():
  391. message = fmt.Sprintf("%s (%s %s)", message, strings.ToLower(verb), qualifiedResource.String())
  392. }
  393. var causes []metav1.StatusCause
  394. if isUnexpectedResponse {
  395. causes = []metav1.StatusCause{
  396. {
  397. Type: metav1.CauseTypeUnexpectedServerResponse,
  398. Message: serverMessage,
  399. },
  400. }
  401. } else {
  402. causes = nil
  403. }
  404. return &StatusError{metav1.Status{
  405. Status: metav1.StatusFailure,
  406. Code: int32(code),
  407. Reason: reason,
  408. Details: &metav1.StatusDetails{
  409. Group: qualifiedResource.Group,
  410. Kind: qualifiedResource.Resource,
  411. Name: name,
  412. Causes: causes,
  413. RetryAfterSeconds: int32(retryAfterSeconds),
  414. },
  415. Message: message,
  416. }}
  417. }
  418. // IsNotFound returns true if the specified error was created by NewNotFound.
  419. func IsNotFound(err error) bool {
  420. return ReasonForError(err) == metav1.StatusReasonNotFound
  421. }
  422. // IsAlreadyExists determines if the err is an error which indicates that a specified resource already exists.
  423. func IsAlreadyExists(err error) bool {
  424. return ReasonForError(err) == metav1.StatusReasonAlreadyExists
  425. }
  426. // IsConflict determines if the err is an error which indicates the provided update conflicts.
  427. func IsConflict(err error) bool {
  428. return ReasonForError(err) == metav1.StatusReasonConflict
  429. }
  430. // IsInvalid determines if the err is an error which indicates the provided resource is not valid.
  431. func IsInvalid(err error) bool {
  432. return ReasonForError(err) == metav1.StatusReasonInvalid
  433. }
  434. // IsGone is true if the error indicates the requested resource is no longer available.
  435. func IsGone(err error) bool {
  436. return ReasonForError(err) == metav1.StatusReasonGone
  437. }
  438. // IsResourceExpired is true if the error indicates the resource has expired and the current action is
  439. // no longer possible.
  440. func IsResourceExpired(err error) bool {
  441. return ReasonForError(err) == metav1.StatusReasonExpired
  442. }
  443. // IsNotAcceptable determines if err is an error which indicates that the request failed due to an invalid Accept header
  444. func IsNotAcceptable(err error) bool {
  445. return ReasonForError(err) == metav1.StatusReasonNotAcceptable
  446. }
  447. // IsUnsupportedMediaType determines if err is an error which indicates that the request failed due to an invalid Content-Type header
  448. func IsUnsupportedMediaType(err error) bool {
  449. return ReasonForError(err) == metav1.StatusReasonUnsupportedMediaType
  450. }
  451. // IsMethodNotSupported determines if the err is an error which indicates the provided action could not
  452. // be performed because it is not supported by the server.
  453. func IsMethodNotSupported(err error) bool {
  454. return ReasonForError(err) == metav1.StatusReasonMethodNotAllowed
  455. }
  456. // IsServiceUnavailable is true if the error indicates the underlying service is no longer available.
  457. func IsServiceUnavailable(err error) bool {
  458. return ReasonForError(err) == metav1.StatusReasonServiceUnavailable
  459. }
  460. // IsBadRequest determines if err is an error which indicates that the request is invalid.
  461. func IsBadRequest(err error) bool {
  462. return ReasonForError(err) == metav1.StatusReasonBadRequest
  463. }
  464. // IsUnauthorized determines if err is an error which indicates that the request is unauthorized and
  465. // requires authentication by the user.
  466. func IsUnauthorized(err error) bool {
  467. return ReasonForError(err) == metav1.StatusReasonUnauthorized
  468. }
  469. // IsForbidden determines if err is an error which indicates that the request is forbidden and cannot
  470. // be completed as requested.
  471. func IsForbidden(err error) bool {
  472. return ReasonForError(err) == metav1.StatusReasonForbidden
  473. }
  474. // IsTimeout determines if err is an error which indicates that request times out due to long
  475. // processing.
  476. func IsTimeout(err error) bool {
  477. return ReasonForError(err) == metav1.StatusReasonTimeout
  478. }
  479. // IsServerTimeout determines if err is an error which indicates that the request needs to be retried
  480. // by the client.
  481. func IsServerTimeout(err error) bool {
  482. return ReasonForError(err) == metav1.StatusReasonServerTimeout
  483. }
  484. // IsInternalError determines if err is an error which indicates an internal server error.
  485. func IsInternalError(err error) bool {
  486. return ReasonForError(err) == metav1.StatusReasonInternalError
  487. }
  488. // IsTooManyRequests determines if err is an error which indicates that there are too many requests
  489. // that the server cannot handle.
  490. func IsTooManyRequests(err error) bool {
  491. if ReasonForError(err) == metav1.StatusReasonTooManyRequests {
  492. return true
  493. }
  494. switch t := err.(type) {
  495. case APIStatus:
  496. return t.Status().Code == http.StatusTooManyRequests
  497. }
  498. return false
  499. }
  500. // IsRequestEntityTooLargeError determines if err is an error which indicates
  501. // the request entity is too large.
  502. func IsRequestEntityTooLargeError(err error) bool {
  503. if ReasonForError(err) == metav1.StatusReasonRequestEntityTooLarge {
  504. return true
  505. }
  506. switch t := err.(type) {
  507. case APIStatus:
  508. return t.Status().Code == http.StatusRequestEntityTooLarge
  509. }
  510. return false
  511. }
  512. // IsUnexpectedServerError returns true if the server response was not in the expected API format,
  513. // and may be the result of another HTTP actor.
  514. func IsUnexpectedServerError(err error) bool {
  515. switch t := err.(type) {
  516. case APIStatus:
  517. if d := t.Status().Details; d != nil {
  518. for _, cause := range d.Causes {
  519. if cause.Type == metav1.CauseTypeUnexpectedServerResponse {
  520. return true
  521. }
  522. }
  523. }
  524. }
  525. return false
  526. }
  527. // IsUnexpectedObjectError determines if err is due to an unexpected object from the master.
  528. func IsUnexpectedObjectError(err error) bool {
  529. _, ok := err.(*UnexpectedObjectError)
  530. return err != nil && ok
  531. }
  532. // SuggestsClientDelay returns true if this error suggests a client delay as well as the
  533. // suggested seconds to wait, or false if the error does not imply a wait. It does not
  534. // address whether the error *should* be retried, since some errors (like a 3xx) may
  535. // request delay without retry.
  536. func SuggestsClientDelay(err error) (int, bool) {
  537. switch t := err.(type) {
  538. case APIStatus:
  539. if t.Status().Details != nil {
  540. switch t.Status().Reason {
  541. // this StatusReason explicitly requests the caller to delay the action
  542. case metav1.StatusReasonServerTimeout:
  543. return int(t.Status().Details.RetryAfterSeconds), true
  544. }
  545. // If the client requests that we retry after a certain number of seconds
  546. if t.Status().Details.RetryAfterSeconds > 0 {
  547. return int(t.Status().Details.RetryAfterSeconds), true
  548. }
  549. }
  550. }
  551. return 0, false
  552. }
  553. // ReasonForError returns the HTTP status for a particular error.
  554. func ReasonForError(err error) metav1.StatusReason {
  555. switch t := err.(type) {
  556. case APIStatus:
  557. return t.Status().Reason
  558. }
  559. return metav1.StatusReasonUnknown
  560. }