error.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package v2error describes errors in etcd project. When any change happens,
  15. // Documentation/v2/errorcode.md needs to be updated correspondingly.
  16. // To be deprecated in favor of v3 APIs.
  17. package v2error
  18. import (
  19. "encoding/json"
  20. "fmt"
  21. "net/http"
  22. )
  23. var errors = map[int]string{
  24. // command related errors
  25. EcodeKeyNotFound: "Key not found",
  26. EcodeTestFailed: "Compare failed", //test and set
  27. EcodeNotFile: "Not a file",
  28. ecodeNoMorePeer: "Reached the max number of peers in the cluster",
  29. EcodeNotDir: "Not a directory",
  30. EcodeNodeExist: "Key already exists", // create
  31. ecodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
  32. EcodeRootROnly: "Root is read only",
  33. EcodeDirNotEmpty: "Directory not empty",
  34. ecodeExistingPeerAddr: "Peer address has existed",
  35. EcodeUnauthorized: "The request requires user authentication",
  36. // Post form related errors
  37. ecodeValueRequired: "Value is Required in POST form",
  38. EcodePrevValueRequired: "PrevValue is Required in POST form",
  39. EcodeTTLNaN: "The given TTL in POST form is not a number",
  40. EcodeIndexNaN: "The given index in POST form is not a number",
  41. ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
  42. ecodeTimeoutNaN: "The given timeout in POST form is not a number",
  43. ecodeNameRequired: "Name is required in POST form",
  44. ecodeIndexOrValueRequired: "Index or value is required",
  45. ecodeIndexValueMutex: "Index and value cannot both be specified",
  46. EcodeInvalidField: "Invalid field",
  47. EcodeInvalidForm: "Invalid POST form",
  48. EcodeRefreshValue: "Value provided on refresh",
  49. EcodeRefreshTTLRequired: "A TTL must be provided on refresh",
  50. // raft related errors
  51. EcodeRaftInternal: "Raft Internal Error",
  52. EcodeLeaderElect: "During Leader Election",
  53. // etcd related errors
  54. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  55. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  56. ecodeStandbyInternal: "Standby Internal Error",
  57. ecodeInvalidActiveSize: "Invalid active size",
  58. ecodeInvalidRemoveDelay: "Standby remove delay",
  59. // client related errors
  60. ecodeClientInternal: "Client Internal Error",
  61. }
  62. var errorStatus = map[int]int{
  63. EcodeKeyNotFound: http.StatusNotFound,
  64. EcodeNotFile: http.StatusForbidden,
  65. EcodeDirNotEmpty: http.StatusForbidden,
  66. EcodeUnauthorized: http.StatusUnauthorized,
  67. EcodeTestFailed: http.StatusPreconditionFailed,
  68. EcodeNodeExist: http.StatusPreconditionFailed,
  69. EcodeRaftInternal: http.StatusInternalServerError,
  70. EcodeLeaderElect: http.StatusInternalServerError,
  71. }
  72. const (
  73. EcodeKeyNotFound = 100
  74. EcodeTestFailed = 101
  75. EcodeNotFile = 102
  76. ecodeNoMorePeer = 103
  77. EcodeNotDir = 104
  78. EcodeNodeExist = 105
  79. ecodeKeyIsPreserved = 106
  80. EcodeRootROnly = 107
  81. EcodeDirNotEmpty = 108
  82. ecodeExistingPeerAddr = 109
  83. EcodeUnauthorized = 110
  84. ecodeValueRequired = 200
  85. EcodePrevValueRequired = 201
  86. EcodeTTLNaN = 202
  87. EcodeIndexNaN = 203
  88. ecodeValueOrTTLRequired = 204
  89. ecodeTimeoutNaN = 205
  90. ecodeNameRequired = 206
  91. ecodeIndexOrValueRequired = 207
  92. ecodeIndexValueMutex = 208
  93. EcodeInvalidField = 209
  94. EcodeInvalidForm = 210
  95. EcodeRefreshValue = 211
  96. EcodeRefreshTTLRequired = 212
  97. EcodeRaftInternal = 300
  98. EcodeLeaderElect = 301
  99. EcodeWatcherCleared = 400
  100. EcodeEventIndexCleared = 401
  101. ecodeStandbyInternal = 402
  102. ecodeInvalidActiveSize = 403
  103. ecodeInvalidRemoveDelay = 404
  104. ecodeClientInternal = 500
  105. )
  106. type Error struct {
  107. ErrorCode int `json:"errorCode"`
  108. Message string `json:"message"`
  109. Cause string `json:"cause,omitempty"`
  110. Index uint64 `json:"index"`
  111. }
  112. func NewRequestError(errorCode int, cause string) *Error {
  113. return NewError(errorCode, cause, 0)
  114. }
  115. func NewError(errorCode int, cause string, index uint64) *Error {
  116. return &Error{
  117. ErrorCode: errorCode,
  118. Message: errors[errorCode],
  119. Cause: cause,
  120. Index: index,
  121. }
  122. }
  123. // Error is for the error interface
  124. func (e Error) Error() string {
  125. return e.Message + " (" + e.Cause + ")"
  126. }
  127. func (e Error) toJsonString() string {
  128. b, _ := json.Marshal(e)
  129. return string(b)
  130. }
  131. func (e Error) StatusCode() int {
  132. status, ok := errorStatus[e.ErrorCode]
  133. if !ok {
  134. status = http.StatusBadRequest
  135. }
  136. return status
  137. }
  138. func (e Error) WriteTo(w http.ResponseWriter) error {
  139. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  140. w.Header().Set("Content-Type", "application/json")
  141. w.WriteHeader(e.StatusCode())
  142. _, err := w.Write([]byte(e.toJsonString() + "\n"))
  143. return err
  144. }