error.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. var errorStatus = map[int]int{
  56. EcodeKeyNotFound: http.StatusNotFound,
  57. EcodeNotFile: http.StatusForbidden,
  58. EcodeDirNotEmpty: http.StatusForbidden,
  59. EcodeTestFailed: http.StatusPreconditionFailed,
  60. EcodeNodeExist: http.StatusPreconditionFailed,
  61. EcodeRaftInternal: http.StatusInternalServerError,
  62. EcodeLeaderElect: http.StatusInternalServerError,
  63. }
  64. const (
  65. EcodeKeyNotFound = 100
  66. EcodeTestFailed = 101
  67. EcodeNotFile = 102
  68. EcodeNoMorePeer = 103
  69. EcodeNotDir = 104
  70. EcodeNodeExist = 105
  71. EcodeKeyIsPreserved = 106
  72. EcodeRootROnly = 107
  73. EcodeDirNotEmpty = 108
  74. EcodeExistingPeerAddr = 109
  75. EcodeValueRequired = 200
  76. EcodePrevValueRequired = 201
  77. EcodeTTLNaN = 202
  78. EcodeIndexNaN = 203
  79. EcodeValueOrTTLRequired = 204
  80. EcodeTimeoutNaN = 205
  81. EcodeNameRequired = 206
  82. EcodeIndexOrValueRequired = 207
  83. EcodeIndexValueMutex = 208
  84. EcodeInvalidField = 209
  85. EcodeInvalidForm = 210
  86. EcodeRaftInternal = 300
  87. EcodeLeaderElect = 301
  88. EcodeWatcherCleared = 400
  89. EcodeEventIndexCleared = 401
  90. EcodeStandbyInternal = 402
  91. EcodeInvalidActiveSize = 403
  92. EcodeInvalidRemoveDelay = 404
  93. EcodeClientInternal = 500
  94. )
  95. type Error struct {
  96. ErrorCode int `json:"errorCode"`
  97. Message string `json:"message"`
  98. Cause string `json:"cause,omitempty"`
  99. Index uint64 `json:"index"`
  100. }
  101. func NewRequestError(errorCode int, cause string) *Error {
  102. return NewError(errorCode, cause, 0)
  103. }
  104. func NewError(errorCode int, cause string, index uint64) *Error {
  105. return &Error{
  106. ErrorCode: errorCode,
  107. Message: errors[errorCode],
  108. Cause: cause,
  109. Index: index,
  110. }
  111. }
  112. func Message(code int) string {
  113. return errors[code]
  114. }
  115. // Only for error interface
  116. func (e Error) Error() string {
  117. return e.Message + " (" + e.Cause + ")"
  118. }
  119. func (e Error) toJsonString() string {
  120. b, _ := json.Marshal(e)
  121. return string(b)
  122. }
  123. func (e Error) statusCode() int {
  124. status, ok := errorStatus[e.ErrorCode]
  125. if !ok {
  126. status = http.StatusBadRequest
  127. }
  128. return status
  129. }
  130. func (e Error) WriteTo(w http.ResponseWriter) {
  131. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  132. w.Header().Set("Content-Type", "application/json")
  133. w.WriteHeader(e.statusCode())
  134. fmt.Fprintln(w, e.toJsonString())
  135. }