errors.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 apiserver
  14. import (
  15. "fmt"
  16. "net/http"
  17. "strings"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. "k8s.io/kubernetes/pkg/storage"
  20. "k8s.io/kubernetes/pkg/util/runtime"
  21. )
  22. // statusError is an object that can be converted into an unversioned.Status
  23. type statusError interface {
  24. Status() unversioned.Status
  25. }
  26. // errToAPIStatus converts an error to an unversioned.Status object.
  27. func errToAPIStatus(err error) *unversioned.Status {
  28. switch t := err.(type) {
  29. case statusError:
  30. status := t.Status()
  31. if len(status.Status) == 0 {
  32. status.Status = unversioned.StatusFailure
  33. }
  34. if status.Code == 0 {
  35. switch status.Status {
  36. case unversioned.StatusSuccess:
  37. status.Code = http.StatusOK
  38. case unversioned.StatusFailure:
  39. status.Code = http.StatusInternalServerError
  40. }
  41. }
  42. //TODO: check for invalid responses
  43. return &status
  44. default:
  45. status := http.StatusInternalServerError
  46. switch {
  47. //TODO: replace me with NewConflictErr
  48. case storage.IsTestFailed(err):
  49. status = http.StatusConflict
  50. }
  51. // Log errors that were not converted to an error status
  52. // by REST storage - these typically indicate programmer
  53. // error by not using pkg/api/errors, or unexpected failure
  54. // cases.
  55. runtime.HandleError(fmt.Errorf("apiserver received an error that is not an unversioned.Status: %v", err))
  56. return &unversioned.Status{
  57. Status: unversioned.StatusFailure,
  58. Code: int32(status),
  59. Reason: unversioned.StatusReasonUnknown,
  60. Message: err.Error(),
  61. }
  62. }
  63. }
  64. // notFound renders a simple not found error.
  65. func notFound(w http.ResponseWriter, req *http.Request) {
  66. w.WriteHeader(http.StatusNotFound)
  67. fmt.Fprintf(w, "Not Found: %#v", req.RequestURI)
  68. }
  69. // badGatewayError renders a simple bad gateway error.
  70. func badGatewayError(w http.ResponseWriter, req *http.Request) {
  71. w.WriteHeader(http.StatusBadGateway)
  72. fmt.Fprintf(w, "Bad Gateway: %#v", req.RequestURI)
  73. }
  74. // forbidden renders a simple forbidden error
  75. func forbidden(w http.ResponseWriter, req *http.Request) {
  76. w.WriteHeader(http.StatusForbidden)
  77. fmt.Fprintf(w, "Forbidden: %#v", req.RequestURI)
  78. }
  79. // internalError renders a simple internal error
  80. func internalError(w http.ResponseWriter, req *http.Request, err error) {
  81. w.WriteHeader(http.StatusInternalServerError)
  82. fmt.Fprintf(w, "Internal Server Error: %#v", req.RequestURI)
  83. runtime.HandleError(err)
  84. }
  85. // errAPIPrefixNotFound indicates that a RequestInfo resolution failed because the request isn't under
  86. // any known API prefixes
  87. type errAPIPrefixNotFound struct {
  88. SpecifiedPrefix string
  89. }
  90. func (e *errAPIPrefixNotFound) Error() string {
  91. return fmt.Sprintf("no valid API prefix found matching %v", e.SpecifiedPrefix)
  92. }
  93. func IsAPIPrefixNotFound(err error) bool {
  94. if err == nil {
  95. return false
  96. }
  97. _, ok := err.(*errAPIPrefixNotFound)
  98. return ok
  99. }
  100. // errNotAcceptable indicates Accept negotiation has failed
  101. // TODO: move to api/errors if other code needs to return this
  102. type errNotAcceptable struct {
  103. accepted []string
  104. }
  105. func (e errNotAcceptable) Error() string {
  106. return fmt.Sprintf("only the following media types are accepted: %v", strings.Join(e.accepted, ", "))
  107. }
  108. func (e errNotAcceptable) Status() unversioned.Status {
  109. return unversioned.Status{
  110. Status: unversioned.StatusFailure,
  111. Code: http.StatusNotAcceptable,
  112. Reason: unversioned.StatusReason("NotAcceptable"),
  113. Message: e.Error(),
  114. }
  115. }
  116. // errUnsupportedMediaType indicates Content-Type is not recognized
  117. // TODO: move to api/errors if other code needs to return this
  118. type errUnsupportedMediaType struct {
  119. accepted []string
  120. }
  121. func (e errUnsupportedMediaType) Error() string {
  122. return fmt.Sprintf("the body of the request was in an unknown format - accepted media types include: %v", strings.Join(e.accepted, ", "))
  123. }
  124. func (e errUnsupportedMediaType) Status() unversioned.Status {
  125. return unversioned.Status{
  126. Status: unversioned.StatusFailure,
  127. Code: http.StatusUnsupportedMediaType,
  128. Reason: unversioned.StatusReason("UnsupportedMediaType"),
  129. Message: e.Error(),
  130. }
  131. }