error.go 4.0 KB

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