error.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2013 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. package error
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net/http"
  18. )
  19. var errors = map[int]string{
  20. // command related errors
  21. EcodeKeyNotFound: "Key not found",
  22. EcodeTestFailed: "Compare failed", //test and set
  23. EcodeNotFile: "Not a file",
  24. EcodeNoMorePeer: "Reached the max number of peers in the cluster",
  25. EcodeNotDir: "Not a directory",
  26. EcodeNodeExist: "Key already exists", // create
  27. EcodeRootROnly: "Root is read only",
  28. EcodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
  29. EcodeDirNotEmpty: "Directory not empty",
  30. EcodeExistingPeerAddr: "Peer address has existed",
  31. // Post form related errors
  32. EcodeValueRequired: "Value is Required in POST form",
  33. EcodePrevValueRequired: "PrevValue is Required in POST form",
  34. EcodeTTLNaN: "The given TTL in POST form is not a number",
  35. EcodeIndexNaN: "The given index in POST form is not a number",
  36. EcodeValueOrTTLRequired: "Value or TTL is required in POST form",
  37. EcodeTimeoutNaN: "The given timeout in POST form is not a number",
  38. EcodeNameRequired: "Name is required in POST form",
  39. EcodeIndexOrValueRequired: "Index or value is required",
  40. EcodeIndexValueMutex: "Index and value cannot both be specified",
  41. EcodeInvalidField: "Invalid field",
  42. // raft related errors
  43. EcodeRaftInternal: "Raft Internal Error",
  44. EcodeLeaderElect: "During Leader Election",
  45. // etcd related errors
  46. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  47. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  48. EcodeStandbyInternal: "Standby Internal Error",
  49. EcodeInvalidActiveSize: "Invalid active size",
  50. EcodeInvalidPromoteDelay: "Standby promote delay",
  51. EcodePromoteError: "Standby promotion error",
  52. // client related errors
  53. EcodeClientInternal: "Client Internal Error",
  54. }
  55. const (
  56. EcodeKeyNotFound = 100
  57. EcodeTestFailed = 101
  58. EcodeNotFile = 102
  59. EcodeNoMorePeer = 103
  60. EcodeNotDir = 104
  61. EcodeNodeExist = 105
  62. EcodeKeyIsPreserved = 106
  63. EcodeRootROnly = 107
  64. EcodeDirNotEmpty = 108
  65. EcodeExistingPeerAddr = 109
  66. EcodeValueRequired = 200
  67. EcodePrevValueRequired = 201
  68. EcodeTTLNaN = 202
  69. EcodeIndexNaN = 203
  70. EcodeValueOrTTLRequired = 204
  71. EcodeTimeoutNaN = 205
  72. EcodeNameRequired = 206
  73. EcodeIndexOrValueRequired = 207
  74. EcodeIndexValueMutex = 208
  75. EcodeInvalidField = 209
  76. EcodeRaftInternal = 300
  77. EcodeLeaderElect = 301
  78. EcodeWatcherCleared = 400
  79. EcodeEventIndexCleared = 401
  80. EcodeStandbyInternal = 402
  81. EcodeInvalidActiveSize = 403
  82. EcodeInvalidPromoteDelay = 404
  83. EcodePromoteError = 405
  84. EcodeClientInternal = 500
  85. )
  86. type Error struct {
  87. ErrorCode int `json:"errorCode"`
  88. Message string `json:"message"`
  89. Cause string `json:"cause,omitempty"`
  90. Index uint64 `json:"index"`
  91. }
  92. func NewError(errorCode int, cause string, index uint64) *Error {
  93. return &Error{
  94. ErrorCode: errorCode,
  95. Message: errors[errorCode],
  96. Cause: cause,
  97. Index: index,
  98. }
  99. }
  100. func Message(code int) string {
  101. return errors[code]
  102. }
  103. // Only for error interface
  104. func (e Error) Error() string {
  105. return e.Message + " (" + e.Cause + ")"
  106. }
  107. func (e Error) toJsonString() string {
  108. b, _ := json.Marshal(e)
  109. return string(b)
  110. }
  111. func (e Error) Write(w http.ResponseWriter) {
  112. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  113. // 3xx is raft internal error
  114. status := http.StatusBadRequest
  115. switch e.ErrorCode {
  116. case EcodeKeyNotFound:
  117. status = http.StatusNotFound
  118. case EcodeNotFile, EcodeDirNotEmpty:
  119. status = http.StatusForbidden
  120. case EcodeTestFailed, EcodeNodeExist:
  121. status = http.StatusPreconditionFailed
  122. default:
  123. if e.ErrorCode/100 == 3 {
  124. status = http.StatusInternalServerError
  125. }
  126. }
  127. http.Error(w, e.toJsonString(), status)
  128. }