error.go 4.3 KB

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