error.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. EcodeValueRequired = 200
  30. EcodePrevValueRequired = 201
  31. EcodeTTLNaN = 202
  32. EcodeIndexNaN = 203
  33. EcodeValueOrTTLRequired = 204
  34. EcodeRaftInternal = 300
  35. EcodeLeaderElect = 301
  36. EcodeWatcherCleared = 400
  37. EcodeEventIndexCleared = 401
  38. )
  39. func init() {
  40. errors = make(map[int]string)
  41. // command related errors
  42. errors[EcodeKeyNotFound] = "Key Not Found"
  43. errors[EcodeTestFailed] = "Test Failed" //test and set
  44. errors[EcodeNotFile] = "Not A File"
  45. errors[EcodeNoMorePeer] = "Reached the max number of peers in the cluster"
  46. errors[EcodeNotDir] = "Not A Directory"
  47. errors[EcodeNodeExist] = "Already exists" // create
  48. errors[EcodeRootROnly] = "Root is read only"
  49. errors[EcodeKeyIsPreserved] = "The prefix of given key is a keyword in etcd"
  50. // Post form related errors
  51. errors[EcodeValueRequired] = "Value is Required in POST form"
  52. errors[EcodePrevValueRequired] = "PrevValue is Required in POST form"
  53. errors[EcodeTTLNaN] = "The given TTL in POST form is not a number"
  54. errors[EcodeIndexNaN] = "The given index in POST form is not a number"
  55. errors[EcodeValueOrTTLRequired] = "Value or TTL is required in POST form"
  56. // raft related errors
  57. errors[EcodeRaftInternal] = "Raft Internal Error"
  58. errors[EcodeLeaderElect] = "During Leader Election"
  59. // etcd related errors
  60. errors[EcodeWatcherCleared] = "watcher is cleared due to etcd recovery"
  61. errors[EcodeEventIndexCleared] = "The event in requested index is outdated and cleared"
  62. }
  63. type Error struct {
  64. ErrorCode int `json:"errorCode"`
  65. Message string `json:"message"`
  66. Cause string `json:"cause,omitempty"`
  67. Index uint64 `json:"index"`
  68. }
  69. func NewError(errorCode int, cause string, index uint64) *Error {
  70. return &Error{
  71. ErrorCode: errorCode,
  72. Message: errors[errorCode],
  73. Cause: cause,
  74. Index: index,
  75. }
  76. }
  77. func Message(code int) string {
  78. return errors[code]
  79. }
  80. // Only for error interface
  81. func (e Error) Error() string {
  82. return e.Message
  83. }
  84. func (e Error) toJsonString() string {
  85. b, _ := json.Marshal(e)
  86. return string(b)
  87. }
  88. func (e Error) Write(w http.ResponseWriter) {
  89. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  90. // 3xx is reft internal error
  91. if e.ErrorCode/100 == 3 {
  92. http.Error(w, e.toJsonString(), http.StatusInternalServerError)
  93. } else {
  94. http.Error(w, e.toJsonString(), http.StatusBadRequest)
  95. }
  96. }