error.go 3.1 KB

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