error.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Post form related errors
  31. EcodeValueRequired: "Value is Required in POST form",
  32. EcodePrevValueRequired: "PrevValue is Required in POST form",
  33. EcodeTTLNaN: "The given TTL in POST form is not a number",
  34. EcodeIndexNaN: "The given index in POST form is not a number",
  35. EcodeValueOrTTLRequired: "Value or TTL is required in POST form",
  36. EcodeTimeoutNaN: "The given timeout in POST form is not a number",
  37. EcodeNameRequired: "Name is required in POST form",
  38. EcodeIndexOrValueRequired: "Index or value is required",
  39. EcodeIndexValueMutex: "Index and value cannot both be specified",
  40. EcodeInvalidField: "Invalid field",
  41. // raft related errors
  42. EcodeRaftInternal: "Raft Internal Error",
  43. EcodeLeaderElect: "During Leader Election",
  44. // etcd related errors
  45. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  46. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  47. EcodeProxyInternal: "Proxy Internal Error",
  48. EcodeInvalidActiveSize: "Invalid active size",
  49. EcodeInvalidPromoteDelay: "Proxy promote delay",
  50. EcodePromoteError: "Proxy promotion error",
  51. }
  52. const (
  53. EcodeKeyNotFound = 100
  54. EcodeTestFailed = 101
  55. EcodeNotFile = 102
  56. EcodeNoMorePeer = 103
  57. EcodeNotDir = 104
  58. EcodeNodeExist = 105
  59. EcodeKeyIsPreserved = 106
  60. EcodeRootROnly = 107
  61. EcodeDirNotEmpty = 108
  62. EcodeValueRequired = 200
  63. EcodePrevValueRequired = 201
  64. EcodeTTLNaN = 202
  65. EcodeIndexNaN = 203
  66. EcodeValueOrTTLRequired = 204
  67. EcodeTimeoutNaN = 205
  68. EcodeNameRequired = 206
  69. EcodeIndexOrValueRequired = 207
  70. EcodeIndexValueMutex = 208
  71. EcodeInvalidField = 209
  72. EcodeRaftInternal = 300
  73. EcodeLeaderElect = 301
  74. EcodeWatcherCleared = 400
  75. EcodeEventIndexCleared = 401
  76. EcodeProxyInternal = 402
  77. EcodeInvalidActiveSize = 403
  78. EcodeInvalidPromoteDelay = 404
  79. EcodePromoteError = 405
  80. )
  81. type Error struct {
  82. ErrorCode int `json:"errorCode"`
  83. Message string `json:"message"`
  84. Cause string `json:"cause,omitempty"`
  85. Index uint64 `json:"index"`
  86. }
  87. func NewError(errorCode int, cause string, index uint64) *Error {
  88. return &Error{
  89. ErrorCode: errorCode,
  90. Message: errors[errorCode],
  91. Cause: cause,
  92. Index: index,
  93. }
  94. }
  95. func Message(code int) string {
  96. return errors[code]
  97. }
  98. // Only for error interface
  99. func (e Error) Error() string {
  100. return e.Message
  101. }
  102. func (e Error) toJsonString() string {
  103. b, _ := json.Marshal(e)
  104. return string(b)
  105. }
  106. func (e Error) Write(w http.ResponseWriter) {
  107. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  108. // 3xx is raft internal error
  109. status := http.StatusBadRequest
  110. switch e.ErrorCode {
  111. case EcodeKeyNotFound:
  112. status = http.StatusNotFound
  113. case EcodeNotFile, EcodeDirNotEmpty:
  114. status = http.StatusForbidden
  115. case EcodeTestFailed, EcodeNodeExist:
  116. status = http.StatusPreconditionFailed
  117. default:
  118. if e.ErrorCode/100 == 3 {
  119. status = http.StatusInternalServerError
  120. }
  121. }
  122. http.Error(w, e.toJsonString(), status)
  123. }