error.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  53. const (
  54. EcodeKeyNotFound = 100
  55. EcodeTestFailed = 101
  56. EcodeNotFile = 102
  57. EcodeNoMorePeer = 103
  58. EcodeNotDir = 104
  59. EcodeNodeExist = 105
  60. EcodeKeyIsPreserved = 106
  61. EcodeRootROnly = 107
  62. EcodeDirNotEmpty = 108
  63. EcodeExistingPeerAddr = 109
  64. EcodeValueRequired = 200
  65. EcodePrevValueRequired = 201
  66. EcodeTTLNaN = 202
  67. EcodeIndexNaN = 203
  68. EcodeValueOrTTLRequired = 204
  69. EcodeTimeoutNaN = 205
  70. EcodeNameRequired = 206
  71. EcodeIndexOrValueRequired = 207
  72. EcodeIndexValueMutex = 208
  73. EcodeInvalidField = 209
  74. EcodeRaftInternal = 300
  75. EcodeLeaderElect = 301
  76. EcodeWatcherCleared = 400
  77. EcodeEventIndexCleared = 401
  78. EcodeStandbyInternal = 402
  79. EcodeInvalidActiveSize = 403
  80. EcodeInvalidPromoteDelay = 404
  81. EcodePromoteError = 405
  82. )
  83. type Error struct {
  84. ErrorCode int `json:"errorCode"`
  85. Message string `json:"message"`
  86. Cause string `json:"cause,omitempty"`
  87. Index uint64 `json:"index"`
  88. }
  89. func NewError(errorCode int, cause string, index uint64) *Error {
  90. return &Error{
  91. ErrorCode: errorCode,
  92. Message: errors[errorCode],
  93. Cause: cause,
  94. Index: index,
  95. }
  96. }
  97. func Message(code int) string {
  98. return errors[code]
  99. }
  100. // Only for error interface
  101. func (e Error) Error() string {
  102. return e.Message
  103. }
  104. func (e Error) toJsonString() string {
  105. b, _ := json.Marshal(e)
  106. return string(b)
  107. }
  108. func (e Error) Write(w http.ResponseWriter) {
  109. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  110. // 3xx is raft internal error
  111. status := http.StatusBadRequest
  112. switch e.ErrorCode {
  113. case EcodeKeyNotFound:
  114. status = http.StatusNotFound
  115. case EcodeNotFile, EcodeDirNotEmpty:
  116. status = http.StatusForbidden
  117. case EcodeTestFailed, EcodeNodeExist:
  118. status = http.StatusPreconditionFailed
  119. default:
  120. if e.ErrorCode/100 == 3 {
  121. status = http.StatusInternalServerError
  122. }
  123. }
  124. http.Error(w, e.toJsonString(), status)
  125. }