error.go 5.1 KB

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