error.go 4.5 KB

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