error.go 3.9 KB

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