errors.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. "errors"
  17. "fmt"
  18. "net/http"
  19. "reflect"
  20. "strings"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/apimachinery/pkg/util/validation/field"
  25. )
  26. // StatusError is an error intended for consumption by a REST API server; it can also be
  27. // reconstructed by clients from a REST response. Public to allow easy type switches.
  28. type StatusError struct {
  29. ErrStatus metav1.Status
  30. }
  31. // APIStatus is exposed by errors that can be converted to an api.Status object
  32. // for finer grained details.
  33. type APIStatus interface {
  34. Status() metav1.Status
  35. }
  36. var _ error = &StatusError{}
  37. var knownReasons = map[metav1.StatusReason]struct{}{
  38. // metav1.StatusReasonUnknown : {}
  39. metav1.StatusReasonUnauthorized: {},
  40. metav1.StatusReasonForbidden: {},
  41. metav1.StatusReasonNotFound: {},
  42. metav1.StatusReasonAlreadyExists: {},
  43. metav1.StatusReasonConflict: {},
  44. metav1.StatusReasonGone: {},
  45. metav1.StatusReasonInvalid: {},
  46. metav1.StatusReasonServerTimeout: {},
  47. metav1.StatusReasonStoreReadError: {},
  48. metav1.StatusReasonTimeout: {},
  49. metav1.StatusReasonTooManyRequests: {},
  50. metav1.StatusReasonBadRequest: {},
  51. metav1.StatusReasonMethodNotAllowed: {},
  52. metav1.StatusReasonNotAcceptable: {},
  53. metav1.StatusReasonRequestEntityTooLarge: {},
  54. metav1.StatusReasonUnsupportedMediaType: {},
  55. metav1.StatusReasonInternalError: {},
  56. metav1.StatusReasonExpired: {},
  57. metav1.StatusReasonServiceUnavailable: {},
  58. }
  59. // Error implements the Error interface.
  60. func (e *StatusError) Error() string {
  61. return e.ErrStatus.Message
  62. }
  63. // Status allows access to e's status without having to know the detailed workings
  64. // of StatusError.
  65. func (e *StatusError) Status() metav1.Status {
  66. return e.ErrStatus
  67. }
  68. // DebugError reports extended info about the error to debug output.
  69. func (e *StatusError) DebugError() (string, []interface{}) {
  70. if out, err := json.MarshalIndent(e.ErrStatus, "", " "); err == nil {
  71. return "server response object: %s", []interface{}{string(out)}
  72. }
  73. return "server response object: %#v", []interface{}{e.ErrStatus}
  74. }
  75. // HasStatusCause returns true if the provided error has a details cause
  76. // with the provided type name.
  77. // It supports wrapped errors and returns false when the error is nil.
  78. func HasStatusCause(err error, name metav1.CauseType) bool {
  79. _, ok := StatusCause(err, name)
  80. return ok
  81. }
  82. // StatusCause returns the named cause from the provided error if it exists and
  83. // the error unwraps to the type APIStatus. Otherwise it returns false.
  84. func StatusCause(err error, name metav1.CauseType) (metav1.StatusCause, bool) {
  85. status, ok := err.(APIStatus)
  86. if (ok || errors.As(err, &status)) && status.Status().Details != nil {
  87. for _, cause := range status.Status().Details.Causes {
  88. if cause.Type == name {
  89. return cause, true
  90. }
  91. }
  92. }
  93. return metav1.StatusCause{}, false
  94. }
  95. // UnexpectedObjectError can be returned by FromObject if it's passed a non-status object.
  96. type UnexpectedObjectError struct {
  97. Object runtime.Object
  98. }
  99. // Error returns an error message describing 'u'.
  100. func (u *UnexpectedObjectError) Error() string {
  101. return fmt.Sprintf("unexpected object: %v", u.Object)
  102. }
  103. // FromObject generates an StatusError from an metav1.Status, if that is the type of obj; otherwise,
  104. // returns an UnexpecteObjectError.
  105. func FromObject(obj runtime.Object) error {
  106. switch t := obj.(type) {
  107. case *metav1.Status:
  108. return &StatusError{ErrStatus: *t}
  109. case runtime.Unstructured:
  110. var status metav1.Status
  111. obj := t.UnstructuredContent()
  112. if !reflect.DeepEqual(obj["kind"], "Status") {
  113. break
  114. }
  115. if err := runtime.DefaultUnstructuredConverter.FromUnstructured(t.UnstructuredContent(), &status); err != nil {
  116. return err
  117. }
  118. if status.APIVersion != "v1" && status.APIVersion != "meta.k8s.io/v1" {
  119. break
  120. }
  121. return &StatusError{ErrStatus: status}
  122. }
  123. return &UnexpectedObjectError{obj}
  124. }
  125. // NewNotFound returns a new error which indicates that the resource of the kind and the name was not found.
  126. func NewNotFound(qualifiedResource schema.GroupResource, name string) *StatusError {
  127. return &StatusError{metav1.Status{
  128. Status: metav1.StatusFailure,
  129. Code: http.StatusNotFound,
  130. Reason: metav1.StatusReasonNotFound,
  131. Details: &metav1.StatusDetails{
  132. Group: qualifiedResource.Group,
  133. Kind: qualifiedResource.Resource,
  134. Name: name,
  135. },
  136. Message: fmt.Sprintf("%s %q not found", qualifiedResource.String(), name),
  137. }}
  138. }
  139. // NewAlreadyExists returns an error indicating the item requested exists by that identifier.
  140. func NewAlreadyExists(qualifiedResource schema.GroupResource, name string) *StatusError {
  141. return &StatusError{metav1.Status{
  142. Status: metav1.StatusFailure,
  143. Code: http.StatusConflict,
  144. Reason: metav1.StatusReasonAlreadyExists,
  145. Details: &metav1.StatusDetails{
  146. Group: qualifiedResource.Group,
  147. Kind: qualifiedResource.Resource,
  148. Name: name,
  149. },
  150. Message: fmt.Sprintf("%s %q already exists", qualifiedResource.String(), name),
  151. }}
  152. }
  153. // NewGenerateNameConflict returns an error indicating the server
  154. // was not able to generate a valid name for a resource.
  155. func NewGenerateNameConflict(qualifiedResource schema.GroupResource, name string, retryAfterSeconds int) *StatusError {
  156. return &StatusError{metav1.Status{
  157. Status: metav1.StatusFailure,
  158. Code: http.StatusConflict,
  159. Reason: metav1.StatusReasonAlreadyExists,
  160. Details: &metav1.StatusDetails{
  161. Group: qualifiedResource.Group,
  162. Kind: qualifiedResource.Resource,
  163. Name: name,
  164. RetryAfterSeconds: int32(retryAfterSeconds),
  165. },
  166. Message: fmt.Sprintf(
  167. "%s %q already exists, the server was not able to generate a unique name for the object",
  168. qualifiedResource.String(), name),
  169. }}
  170. }
  171. // NewUnauthorized returns an error indicating the client is not authorized to perform the requested
  172. // action.
  173. func NewUnauthorized(reason string) *StatusError {
  174. message := reason
  175. if len(message) == 0 {
  176. message = "not authorized"
  177. }
  178. return &StatusError{metav1.Status{
  179. Status: metav1.StatusFailure,
  180. Code: http.StatusUnauthorized,
  181. Reason: metav1.StatusReasonUnauthorized,
  182. Message: message,
  183. }}
  184. }
  185. // NewForbidden returns an error indicating the requested action was forbidden
  186. func NewForbidden(qualifiedResource schema.GroupResource, name string, err error) *StatusError {
  187. var message string
  188. if qualifiedResource.Empty() {
  189. message = fmt.Sprintf("forbidden: %v", err)
  190. } else if name == "" {
  191. message = fmt.Sprintf("%s is forbidden: %v", qualifiedResource.String(), err)
  192. } else {
  193. message = fmt.Sprintf("%s %q is forbidden: %v", qualifiedResource.String(), name, err)
  194. }
  195. return &StatusError{metav1.Status{
  196. Status: metav1.StatusFailure,
  197. Code: http.StatusForbidden,
  198. Reason: metav1.StatusReasonForbidden,
  199. Details: &metav1.StatusDetails{
  200. Group: qualifiedResource.Group,
  201. Kind: qualifiedResource.Resource,
  202. Name: name,
  203. },
  204. Message: message,
  205. }}
  206. }
  207. // NewConflict returns an error indicating the item can't be updated as provided.
  208. func NewConflict(qualifiedResource schema.GroupResource, name string, err error) *StatusError {
  209. return &StatusError{metav1.Status{
  210. Status: metav1.StatusFailure,
  211. Code: http.StatusConflict,
  212. Reason: metav1.StatusReasonConflict,
  213. Details: &metav1.StatusDetails{
  214. Group: qualifiedResource.Group,
  215. Kind: qualifiedResource.Resource,
  216. Name: name,
  217. },
  218. Message: fmt.Sprintf("Operation cannot be fulfilled on %s %q: %v", qualifiedResource.String(), name, err),
  219. }}
  220. }
  221. // NewApplyConflict returns an error including details on the requests apply conflicts
  222. func NewApplyConflict(causes []metav1.StatusCause, message string) *StatusError {
  223. return &StatusError{ErrStatus: metav1.Status{
  224. Status: metav1.StatusFailure,
  225. Code: http.StatusConflict,
  226. Reason: metav1.StatusReasonConflict,
  227. Details: &metav1.StatusDetails{
  228. // TODO: Get obj details here?
  229. Causes: causes,
  230. },
  231. Message: message,
  232. }}
  233. }
  234. // NewGone returns an error indicating the item no longer available at the server and no forwarding address is known.
  235. //
  236. // Deprecated: Please use NewResourceExpired instead.
  237. func NewGone(message string) *StatusError {
  238. return &StatusError{metav1.Status{
  239. Status: metav1.StatusFailure,
  240. Code: http.StatusGone,
  241. Reason: metav1.StatusReasonGone,
  242. Message: message,
  243. }}
  244. }
  245. // NewResourceExpired creates an error that indicates that the requested resource content has expired from
  246. // the server (usually due to a resourceVersion that is too old).
  247. func NewResourceExpired(message string) *StatusError {
  248. return &StatusError{metav1.Status{
  249. Status: metav1.StatusFailure,
  250. Code: http.StatusGone,
  251. Reason: metav1.StatusReasonExpired,
  252. Message: message,
  253. }}
  254. }
  255. // NewInvalid returns an error indicating the item is invalid and cannot be processed.
  256. func NewInvalid(qualifiedKind schema.GroupKind, name string, errs field.ErrorList) *StatusError {
  257. causes := make([]metav1.StatusCause, 0, len(errs))
  258. for i := range errs {
  259. err := errs[i]
  260. causes = append(causes, metav1.StatusCause{
  261. Type: metav1.CauseType(err.Type),
  262. Message: err.ErrorBody(),
  263. Field: err.Field,
  264. })
  265. }
  266. err := &StatusError{metav1.Status{
  267. Status: metav1.StatusFailure,
  268. Code: http.StatusUnprocessableEntity,
  269. Reason: metav1.StatusReasonInvalid,
  270. Details: &metav1.StatusDetails{
  271. Group: qualifiedKind.Group,
  272. Kind: qualifiedKind.Kind,
  273. Name: name,
  274. Causes: causes,
  275. },
  276. }}
  277. aggregatedErrs := errs.ToAggregate()
  278. if aggregatedErrs == nil {
  279. err.ErrStatus.Message = fmt.Sprintf("%s %q is invalid", qualifiedKind.String(), name)
  280. } else {
  281. err.ErrStatus.Message = fmt.Sprintf("%s %q is invalid: %v", qualifiedKind.String(), name, aggregatedErrs)
  282. }
  283. return err
  284. }
  285. // NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
  286. func NewBadRequest(reason string) *StatusError {
  287. return &StatusError{metav1.Status{
  288. Status: metav1.StatusFailure,
  289. Code: http.StatusBadRequest,
  290. Reason: metav1.StatusReasonBadRequest,
  291. Message: reason,
  292. }}
  293. }
  294. // NewTooManyRequests creates an error that indicates that the client must try again later because
  295. // the specified endpoint is not accepting requests. More specific details should be provided
  296. // if client should know why the failure was limited.
  297. func NewTooManyRequests(message string, retryAfterSeconds int) *StatusError {
  298. return &StatusError{metav1.Status{
  299. Status: metav1.StatusFailure,
  300. Code: http.StatusTooManyRequests,
  301. Reason: metav1.StatusReasonTooManyRequests,
  302. Message: message,
  303. Details: &metav1.StatusDetails{
  304. RetryAfterSeconds: int32(retryAfterSeconds),
  305. },
  306. }}
  307. }
  308. // NewServiceUnavailable creates an error that indicates that the requested service is unavailable.
  309. func NewServiceUnavailable(reason string) *StatusError {
  310. return &StatusError{metav1.Status{
  311. Status: metav1.StatusFailure,
  312. Code: http.StatusServiceUnavailable,
  313. Reason: metav1.StatusReasonServiceUnavailable,
  314. Message: reason,
  315. }}
  316. }
  317. // NewMethodNotSupported returns an error indicating the requested action is not supported on this kind.
  318. func NewMethodNotSupported(qualifiedResource schema.GroupResource, action string) *StatusError {
  319. return &StatusError{metav1.Status{
  320. Status: metav1.StatusFailure,
  321. Code: http.StatusMethodNotAllowed,
  322. Reason: metav1.StatusReasonMethodNotAllowed,
  323. Details: &metav1.StatusDetails{
  324. Group: qualifiedResource.Group,
  325. Kind: qualifiedResource.Resource,
  326. },
  327. Message: fmt.Sprintf("%s is not supported on resources of kind %q", action, qualifiedResource.String()),
  328. }}
  329. }
  330. // NewServerTimeout returns an error indicating the requested action could not be completed due to a
  331. // transient error, and the client should try again.
  332. func NewServerTimeout(qualifiedResource schema.GroupResource, operation string, retryAfterSeconds int) *StatusError {
  333. return &StatusError{metav1.Status{
  334. Status: metav1.StatusFailure,
  335. Code: http.StatusInternalServerError,
  336. Reason: metav1.StatusReasonServerTimeout,
  337. Details: &metav1.StatusDetails{
  338. Group: qualifiedResource.Group,
  339. Kind: qualifiedResource.Resource,
  340. Name: operation,
  341. RetryAfterSeconds: int32(retryAfterSeconds),
  342. },
  343. Message: fmt.Sprintf("The %s operation against %s could not be completed at this time, please try again.", operation, qualifiedResource.String()),
  344. }}
  345. }
  346. // NewServerTimeoutForKind should not exist. Server timeouts happen when accessing resources, the Kind is just what we
  347. // happened to be looking at when the request failed. This delegates to keep code sane, but we should work towards removing this.
  348. func NewServerTimeoutForKind(qualifiedKind schema.GroupKind, operation string, retryAfterSeconds int) *StatusError {
  349. return NewServerTimeout(schema.GroupResource{Group: qualifiedKind.Group, Resource: qualifiedKind.Kind}, operation, retryAfterSeconds)
  350. }
  351. // NewInternalError returns an error indicating the item is invalid and cannot be processed.
  352. func NewInternalError(err error) *StatusError {
  353. return &StatusError{metav1.Status{
  354. Status: metav1.StatusFailure,
  355. Code: http.StatusInternalServerError,
  356. Reason: metav1.StatusReasonInternalError,
  357. Details: &metav1.StatusDetails{
  358. Causes: []metav1.StatusCause{{Message: err.Error()}},
  359. },
  360. Message: fmt.Sprintf("Internal error occurred: %v", err),
  361. }}
  362. }
  363. // NewTimeoutError returns an error indicating that a timeout occurred before the request
  364. // could be completed. Clients may retry, but the operation may still complete.
  365. func NewTimeoutError(message string, retryAfterSeconds int) *StatusError {
  366. return &StatusError{metav1.Status{
  367. Status: metav1.StatusFailure,
  368. Code: http.StatusGatewayTimeout,
  369. Reason: metav1.StatusReasonTimeout,
  370. Message: fmt.Sprintf("Timeout: %s", message),
  371. Details: &metav1.StatusDetails{
  372. RetryAfterSeconds: int32(retryAfterSeconds),
  373. },
  374. }}
  375. }
  376. // NewTooManyRequestsError returns an error indicating that the request was rejected because
  377. // the server has received too many requests. Client should wait and retry. But if the request
  378. // is perishable, then the client should not retry the request.
  379. func NewTooManyRequestsError(message string) *StatusError {
  380. return &StatusError{metav1.Status{
  381. Status: metav1.StatusFailure,
  382. Code: http.StatusTooManyRequests,
  383. Reason: metav1.StatusReasonTooManyRequests,
  384. Message: fmt.Sprintf("Too many requests: %s", message),
  385. }}
  386. }
  387. // NewRequestEntityTooLargeError returns an error indicating that the request
  388. // entity was too large.
  389. func NewRequestEntityTooLargeError(message string) *StatusError {
  390. return &StatusError{metav1.Status{
  391. Status: metav1.StatusFailure,
  392. Code: http.StatusRequestEntityTooLarge,
  393. Reason: metav1.StatusReasonRequestEntityTooLarge,
  394. Message: fmt.Sprintf("Request entity too large: %s", message),
  395. }}
  396. }
  397. // NewGenericServerResponse returns a new error for server responses that are not in a recognizable form.
  398. func NewGenericServerResponse(code int, verb string, qualifiedResource schema.GroupResource, name, serverMessage string, retryAfterSeconds int, isUnexpectedResponse bool) *StatusError {
  399. reason := metav1.StatusReasonUnknown
  400. message := fmt.Sprintf("the server responded with the status code %d but did not return more information", code)
  401. switch code {
  402. case http.StatusConflict:
  403. if verb == http.MethodPost {
  404. reason = metav1.StatusReasonAlreadyExists
  405. } else {
  406. reason = metav1.StatusReasonConflict
  407. }
  408. message = "the server reported a conflict"
  409. case http.StatusNotFound:
  410. reason = metav1.StatusReasonNotFound
  411. message = "the server could not find the requested resource"
  412. case http.StatusBadRequest:
  413. reason = metav1.StatusReasonBadRequest
  414. message = "the server rejected our request for an unknown reason"
  415. case http.StatusUnauthorized:
  416. reason = metav1.StatusReasonUnauthorized
  417. message = "the server has asked for the client to provide credentials"
  418. case http.StatusForbidden:
  419. reason = metav1.StatusReasonForbidden
  420. // the server message has details about who is trying to perform what action. Keep its message.
  421. message = serverMessage
  422. case http.StatusNotAcceptable:
  423. reason = metav1.StatusReasonNotAcceptable
  424. // the server message has details about what types are acceptable
  425. if len(serverMessage) == 0 || serverMessage == "unknown" {
  426. message = "the server was unable to respond with a content type that the client supports"
  427. } else {
  428. message = serverMessage
  429. }
  430. case http.StatusUnsupportedMediaType:
  431. reason = metav1.StatusReasonUnsupportedMediaType
  432. // the server message has details about what types are acceptable
  433. message = serverMessage
  434. case http.StatusMethodNotAllowed:
  435. reason = metav1.StatusReasonMethodNotAllowed
  436. message = "the server does not allow this method on the requested resource"
  437. case http.StatusUnprocessableEntity:
  438. reason = metav1.StatusReasonInvalid
  439. message = "the server rejected our request due to an error in our request"
  440. case http.StatusServiceUnavailable:
  441. reason = metav1.StatusReasonServiceUnavailable
  442. message = "the server is currently unable to handle the request"
  443. case http.StatusGatewayTimeout:
  444. reason = metav1.StatusReasonTimeout
  445. message = "the server was unable to return a response in the time allotted, but may still be processing the request"
  446. case http.StatusTooManyRequests:
  447. reason = metav1.StatusReasonTooManyRequests
  448. message = "the server has received too many requests and has asked us to try again later"
  449. default:
  450. if code >= 500 {
  451. reason = metav1.StatusReasonInternalError
  452. message = fmt.Sprintf("an error on the server (%q) has prevented the request from succeeding", serverMessage)
  453. }
  454. }
  455. switch {
  456. case !qualifiedResource.Empty() && len(name) > 0:
  457. message = fmt.Sprintf("%s (%s %s %s)", message, strings.ToLower(verb), qualifiedResource.String(), name)
  458. case !qualifiedResource.Empty():
  459. message = fmt.Sprintf("%s (%s %s)", message, strings.ToLower(verb), qualifiedResource.String())
  460. }
  461. var causes []metav1.StatusCause
  462. if isUnexpectedResponse {
  463. causes = []metav1.StatusCause{
  464. {
  465. Type: metav1.CauseTypeUnexpectedServerResponse,
  466. Message: serverMessage,
  467. },
  468. }
  469. } else {
  470. causes = nil
  471. }
  472. return &StatusError{metav1.Status{
  473. Status: metav1.StatusFailure,
  474. Code: int32(code),
  475. Reason: reason,
  476. Details: &metav1.StatusDetails{
  477. Group: qualifiedResource.Group,
  478. Kind: qualifiedResource.Resource,
  479. Name: name,
  480. Causes: causes,
  481. RetryAfterSeconds: int32(retryAfterSeconds),
  482. },
  483. Message: message,
  484. }}
  485. }
  486. // IsNotFound returns true if the specified error was created by NewNotFound.
  487. // It supports wrapped errors and returns false when the error is nil.
  488. func IsNotFound(err error) bool {
  489. reason, code := reasonAndCodeForError(err)
  490. if reason == metav1.StatusReasonNotFound {
  491. return true
  492. }
  493. if _, ok := knownReasons[reason]; !ok && code == http.StatusNotFound {
  494. return true
  495. }
  496. return false
  497. }
  498. // IsAlreadyExists determines if the err is an error which indicates that a specified resource already exists.
  499. // It supports wrapped errors and returns false when the error is nil.
  500. func IsAlreadyExists(err error) bool {
  501. return ReasonForError(err) == metav1.StatusReasonAlreadyExists
  502. }
  503. // IsConflict determines if the err is an error which indicates the provided update conflicts.
  504. // It supports wrapped errors and returns false when the error is nil.
  505. func IsConflict(err error) bool {
  506. reason, code := reasonAndCodeForError(err)
  507. if reason == metav1.StatusReasonConflict {
  508. return true
  509. }
  510. if _, ok := knownReasons[reason]; !ok && code == http.StatusConflict {
  511. return true
  512. }
  513. return false
  514. }
  515. // IsInvalid determines if the err is an error which indicates the provided resource is not valid.
  516. // It supports wrapped errors and returns false when the error is nil.
  517. func IsInvalid(err error) bool {
  518. reason, code := reasonAndCodeForError(err)
  519. if reason == metav1.StatusReasonInvalid {
  520. return true
  521. }
  522. if _, ok := knownReasons[reason]; !ok && code == http.StatusUnprocessableEntity {
  523. return true
  524. }
  525. return false
  526. }
  527. // IsGone is true if the error indicates the requested resource is no longer available.
  528. // It supports wrapped errors and returns false when the error is nil.
  529. func IsGone(err error) bool {
  530. reason, code := reasonAndCodeForError(err)
  531. if reason == metav1.StatusReasonGone {
  532. return true
  533. }
  534. if _, ok := knownReasons[reason]; !ok && code == http.StatusGone {
  535. return true
  536. }
  537. return false
  538. }
  539. // IsResourceExpired is true if the error indicates the resource has expired and the current action is
  540. // no longer possible.
  541. // It supports wrapped errors and returns false when the error is nil.
  542. func IsResourceExpired(err error) bool {
  543. return ReasonForError(err) == metav1.StatusReasonExpired
  544. }
  545. // IsNotAcceptable determines if err is an error which indicates that the request failed due to an invalid Accept header
  546. // It supports wrapped errors and returns false when the error is nil.
  547. func IsNotAcceptable(err error) bool {
  548. reason, code := reasonAndCodeForError(err)
  549. if reason == metav1.StatusReasonNotAcceptable {
  550. return true
  551. }
  552. if _, ok := knownReasons[reason]; !ok && code == http.StatusNotAcceptable {
  553. return true
  554. }
  555. return false
  556. }
  557. // IsUnsupportedMediaType determines if err is an error which indicates that the request failed due to an invalid Content-Type header
  558. // It supports wrapped errors and returns false when the error is nil.
  559. func IsUnsupportedMediaType(err error) bool {
  560. reason, code := reasonAndCodeForError(err)
  561. if reason == metav1.StatusReasonUnsupportedMediaType {
  562. return true
  563. }
  564. if _, ok := knownReasons[reason]; !ok && code == http.StatusUnsupportedMediaType {
  565. return true
  566. }
  567. return false
  568. }
  569. // IsMethodNotSupported determines if the err is an error which indicates the provided action could not
  570. // be performed because it is not supported by the server.
  571. // It supports wrapped errors and returns false when the error is nil.
  572. func IsMethodNotSupported(err error) bool {
  573. reason, code := reasonAndCodeForError(err)
  574. if reason == metav1.StatusReasonMethodNotAllowed {
  575. return true
  576. }
  577. if _, ok := knownReasons[reason]; !ok && code == http.StatusMethodNotAllowed {
  578. return true
  579. }
  580. return false
  581. }
  582. // IsServiceUnavailable is true if the error indicates the underlying service is no longer available.
  583. // It supports wrapped errors and returns false when the error is nil.
  584. func IsServiceUnavailable(err error) bool {
  585. reason, code := reasonAndCodeForError(err)
  586. if reason == metav1.StatusReasonServiceUnavailable {
  587. return true
  588. }
  589. if _, ok := knownReasons[reason]; !ok && code == http.StatusServiceUnavailable {
  590. return true
  591. }
  592. return false
  593. }
  594. // IsBadRequest determines if err is an error which indicates that the request is invalid.
  595. // It supports wrapped errors and returns false when the error is nil.
  596. func IsBadRequest(err error) bool {
  597. reason, code := reasonAndCodeForError(err)
  598. if reason == metav1.StatusReasonBadRequest {
  599. return true
  600. }
  601. if _, ok := knownReasons[reason]; !ok && code == http.StatusBadRequest {
  602. return true
  603. }
  604. return false
  605. }
  606. // IsUnauthorized determines if err is an error which indicates that the request is unauthorized and
  607. // requires authentication by the user.
  608. // It supports wrapped errors and returns false when the error is nil.
  609. func IsUnauthorized(err error) bool {
  610. reason, code := reasonAndCodeForError(err)
  611. if reason == metav1.StatusReasonUnauthorized {
  612. return true
  613. }
  614. if _, ok := knownReasons[reason]; !ok && code == http.StatusUnauthorized {
  615. return true
  616. }
  617. return false
  618. }
  619. // IsForbidden determines if err is an error which indicates that the request is forbidden and cannot
  620. // be completed as requested.
  621. // It supports wrapped errors and returns false when the error is nil.
  622. func IsForbidden(err error) bool {
  623. reason, code := reasonAndCodeForError(err)
  624. if reason == metav1.StatusReasonForbidden {
  625. return true
  626. }
  627. if _, ok := knownReasons[reason]; !ok && code == http.StatusForbidden {
  628. return true
  629. }
  630. return false
  631. }
  632. // IsTimeout determines if err is an error which indicates that request times out due to long
  633. // processing.
  634. // It supports wrapped errors and returns false when the error is nil.
  635. func IsTimeout(err error) bool {
  636. reason, code := reasonAndCodeForError(err)
  637. if reason == metav1.StatusReasonTimeout {
  638. return true
  639. }
  640. if _, ok := knownReasons[reason]; !ok && code == http.StatusGatewayTimeout {
  641. return true
  642. }
  643. return false
  644. }
  645. // IsServerTimeout determines if err is an error which indicates that the request needs to be retried
  646. // by the client.
  647. // It supports wrapped errors and returns false when the error is nil.
  648. func IsServerTimeout(err error) bool {
  649. // do not check the status code, because no https status code exists that can
  650. // be scoped to retryable timeouts.
  651. return ReasonForError(err) == metav1.StatusReasonServerTimeout
  652. }
  653. // IsInternalError determines if err is an error which indicates an internal server error.
  654. // It supports wrapped errors and returns false when the error is nil.
  655. func IsInternalError(err error) bool {
  656. reason, code := reasonAndCodeForError(err)
  657. if reason == metav1.StatusReasonInternalError {
  658. return true
  659. }
  660. if _, ok := knownReasons[reason]; !ok && code == http.StatusInternalServerError {
  661. return true
  662. }
  663. return false
  664. }
  665. // IsTooManyRequests determines if err is an error which indicates that there are too many requests
  666. // that the server cannot handle.
  667. // It supports wrapped errors and returns false when the error is nil.
  668. func IsTooManyRequests(err error) bool {
  669. reason, code := reasonAndCodeForError(err)
  670. if reason == metav1.StatusReasonTooManyRequests {
  671. return true
  672. }
  673. // IsTooManyRequests' checking of code predates the checking of the code in
  674. // the other Is* functions. In order to maintain backward compatibility, this
  675. // does not check that the reason is unknown.
  676. if code == http.StatusTooManyRequests {
  677. return true
  678. }
  679. return false
  680. }
  681. // IsRequestEntityTooLargeError determines if err is an error which indicates
  682. // the request entity is too large.
  683. // It supports wrapped errors and returns false when the error is nil.
  684. func IsRequestEntityTooLargeError(err error) bool {
  685. reason, code := reasonAndCodeForError(err)
  686. if reason == metav1.StatusReasonRequestEntityTooLarge {
  687. return true
  688. }
  689. // IsRequestEntityTooLargeError's checking of code predates the checking of
  690. // the code in the other Is* functions. In order to maintain backward
  691. // compatibility, this does not check that the reason is unknown.
  692. if code == http.StatusRequestEntityTooLarge {
  693. return true
  694. }
  695. return false
  696. }
  697. // IsUnexpectedServerError returns true if the server response was not in the expected API format,
  698. // and may be the result of another HTTP actor.
  699. // It supports wrapped errors and returns false when the error is nil.
  700. func IsUnexpectedServerError(err error) bool {
  701. status, ok := err.(APIStatus)
  702. if (ok || errors.As(err, &status)) && status.Status().Details != nil {
  703. for _, cause := range status.Status().Details.Causes {
  704. if cause.Type == metav1.CauseTypeUnexpectedServerResponse {
  705. return true
  706. }
  707. }
  708. }
  709. return false
  710. }
  711. // IsUnexpectedObjectError determines if err is due to an unexpected object from the master.
  712. // It supports wrapped errors and returns false when the error is nil.
  713. func IsUnexpectedObjectError(err error) bool {
  714. uoe, ok := err.(*UnexpectedObjectError)
  715. return err != nil && (ok || errors.As(err, &uoe))
  716. }
  717. // IsStoreReadError determines if err is due to either failure to transform the
  718. // data from the storage, or failure to decode the object appropriately.
  719. func IsStoreReadError(err error) bool {
  720. return ReasonForError(err) == metav1.StatusReasonStoreReadError
  721. }
  722. // SuggestsClientDelay returns true if this error suggests a client delay as well as the
  723. // suggested seconds to wait, or false if the error does not imply a wait. It does not
  724. // address whether the error *should* be retried, since some errors (like a 3xx) may
  725. // request delay without retry.
  726. // It supports wrapped errors and returns false when the error is nil.
  727. func SuggestsClientDelay(err error) (int, bool) {
  728. t, ok := err.(APIStatus)
  729. if (ok || errors.As(err, &t)) && t.Status().Details != nil {
  730. switch t.Status().Reason {
  731. // this StatusReason explicitly requests the caller to delay the action
  732. case metav1.StatusReasonServerTimeout:
  733. return int(t.Status().Details.RetryAfterSeconds), true
  734. }
  735. // If the client requests that we retry after a certain number of seconds
  736. if t.Status().Details.RetryAfterSeconds > 0 {
  737. return int(t.Status().Details.RetryAfterSeconds), true
  738. }
  739. }
  740. return 0, false
  741. }
  742. // ReasonForError returns the HTTP status for a particular error.
  743. // It supports wrapped errors and returns StatusReasonUnknown when
  744. // the error is nil or doesn't have a status.
  745. func ReasonForError(err error) metav1.StatusReason {
  746. if status, ok := err.(APIStatus); ok || errors.As(err, &status) {
  747. return status.Status().Reason
  748. }
  749. return metav1.StatusReasonUnknown
  750. }
  751. func reasonAndCodeForError(err error) (metav1.StatusReason, int32) {
  752. if status, ok := err.(APIStatus); ok || errors.As(err, &status) {
  753. return status.Status().Reason, status.Status().Code
  754. }
  755. return metav1.StatusReasonUnknown, 0
  756. }
  757. // ErrorReporter converts generic errors into runtime.Object errors without
  758. // requiring the caller to take a dependency on meta/v1 (where Status lives).
  759. // This prevents circular dependencies in core watch code.
  760. type ErrorReporter struct {
  761. code int
  762. verb string
  763. reason string
  764. }
  765. // NewClientErrorReporter will respond with valid v1.Status objects that report
  766. // unexpected server responses. Primarily used by watch to report errors when
  767. // we attempt to decode a response from the server and it is not in the form
  768. // we expect. Because watch is a dependency of the core api, we can't return
  769. // meta/v1.Status in that package and so much inject this interface to convert a
  770. // generic error as appropriate. The reason is passed as a unique status cause
  771. // on the returned status, otherwise the generic "ClientError" is returned.
  772. func NewClientErrorReporter(code int, verb string, reason string) *ErrorReporter {
  773. return &ErrorReporter{
  774. code: code,
  775. verb: verb,
  776. reason: reason,
  777. }
  778. }
  779. // AsObject returns a valid error runtime.Object (a v1.Status) for the given
  780. // error, using the code and verb of the reporter type. The error is set to
  781. // indicate that this was an unexpected server response.
  782. func (r *ErrorReporter) AsObject(err error) runtime.Object {
  783. status := NewGenericServerResponse(r.code, r.verb, schema.GroupResource{}, "", err.Error(), 0, true)
  784. if status.ErrStatus.Details == nil {
  785. status.ErrStatus.Details = &metav1.StatusDetails{}
  786. }
  787. reason := r.reason
  788. if len(reason) == 0 {
  789. reason = "ClientError"
  790. }
  791. status.ErrStatus.Details.Causes = append(status.ErrStatus.Details.Causes, metav1.StatusCause{
  792. Type: metav1.CauseType(reason),
  793. Message: err.Error(),
  794. })
  795. return &status.ErrStatus
  796. }