error.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. // error package describes errors in etcd project.
  14. // When any change happens, Documentation/errorcode.md needs to be updated
  15. // 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. // Post form related errors
  35. ecodeValueRequired: "Value is Required in POST form",
  36. EcodePrevValueRequired: "PrevValue is Required in POST form",
  37. EcodeTTLNaN: "The given TTL in POST form is not a number",
  38. EcodeIndexNaN: "The given index in POST form is not a number",
  39. ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
  40. ecodeTimeoutNaN: "The given timeout in POST form is not a number",
  41. ecodeNameRequired: "Name is required in POST form",
  42. ecodeIndexOrValueRequired: "Index or value is required",
  43. ecodeIndexValueMutex: "Index and value cannot both be specified",
  44. EcodeInvalidField: "Invalid field",
  45. EcodeInvalidForm: "Invalid POST form",
  46. // raft related errors
  47. EcodeRaftInternal: "Raft Internal Error",
  48. EcodeLeaderElect: "During Leader Election",
  49. // etcd related errors
  50. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  51. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  52. ecodeStandbyInternal: "Standby Internal Error",
  53. ecodeInvalidActiveSize: "Invalid active size",
  54. ecodeInvalidRemoveDelay: "Standby remove delay",
  55. // client related errors
  56. ecodeClientInternal: "Client Internal Error",
  57. }
  58. var errorStatus = map[int]int{
  59. EcodeKeyNotFound: http.StatusNotFound,
  60. EcodeNotFile: http.StatusForbidden,
  61. EcodeDirNotEmpty: http.StatusForbidden,
  62. EcodeTestFailed: http.StatusPreconditionFailed,
  63. EcodeNodeExist: http.StatusPreconditionFailed,
  64. EcodeRaftInternal: http.StatusInternalServerError,
  65. EcodeLeaderElect: http.StatusInternalServerError,
  66. }
  67. const (
  68. EcodeKeyNotFound = 100
  69. EcodeTestFailed = 101
  70. EcodeNotFile = 102
  71. ecodeNoMorePeer = 103
  72. EcodeNotDir = 104
  73. EcodeNodeExist = 105
  74. ecodeKeyIsPreserved = 106
  75. EcodeRootROnly = 107
  76. EcodeDirNotEmpty = 108
  77. ecodeExistingPeerAddr = 109
  78. ecodeValueRequired = 200
  79. EcodePrevValueRequired = 201
  80. EcodeTTLNaN = 202
  81. EcodeIndexNaN = 203
  82. ecodeValueOrTTLRequired = 204
  83. ecodeTimeoutNaN = 205
  84. ecodeNameRequired = 206
  85. ecodeIndexOrValueRequired = 207
  86. ecodeIndexValueMutex = 208
  87. EcodeInvalidField = 209
  88. EcodeInvalidForm = 210
  89. EcodeRaftInternal = 300
  90. EcodeLeaderElect = 301
  91. EcodeWatcherCleared = 400
  92. EcodeEventIndexCleared = 401
  93. ecodeStandbyInternal = 402
  94. ecodeInvalidActiveSize = 403
  95. ecodeInvalidRemoveDelay = 404
  96. ecodeClientInternal = 500
  97. )
  98. type Error struct {
  99. ErrorCode int `json:"errorCode"`
  100. Message string `json:"message"`
  101. Cause string `json:"cause,omitempty"`
  102. Index uint64 `json:"index"`
  103. }
  104. func NewRequestError(errorCode int, cause string) *Error {
  105. return NewError(errorCode, cause, 0)
  106. }
  107. func NewError(errorCode int, cause string, index uint64) *Error {
  108. return &Error{
  109. ErrorCode: errorCode,
  110. Message: errors[errorCode],
  111. Cause: cause,
  112. Index: index,
  113. }
  114. }
  115. // Only for error interface
  116. func (e Error) Error() string {
  117. return e.Message + " (" + e.Cause + ")"
  118. }
  119. func (e Error) toJsonString() string {
  120. b, _ := json.Marshal(e)
  121. return string(b)
  122. }
  123. func (e Error) statusCode() int {
  124. status, ok := errorStatus[e.ErrorCode]
  125. if !ok {
  126. status = http.StatusBadRequest
  127. }
  128. return status
  129. }
  130. func (e Error) WriteTo(w http.ResponseWriter) {
  131. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  132. w.Header().Set("Content-Type", "application/json")
  133. w.WriteHeader(e.statusCode())
  134. fmt.Fprintln(w, e.toJsonString())
  135. }