error.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // error package describes errors in etcd project.
  15. // When any change happens, Documentation/errorcode.md needs to be updated
  16. // correspondingly.
  17. package error
  18. import (
  19. "encoding/json"
  20. "fmt"
  21. "net/http"
  22. )
  23. var errors = map[int]string{
  24. // command related errors
  25. EcodeKeyNotFound: "Key not found",
  26. EcodeTestFailed: "Compare failed", //test and set
  27. EcodeNotFile: "Not a file",
  28. ecodeNoMorePeer: "Reached the max number of peers in the cluster",
  29. EcodeNotDir: "Not a directory",
  30. EcodeNodeExist: "Key already exists", // create
  31. ecodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
  32. EcodeRootROnly: "Root is read only",
  33. EcodeDirNotEmpty: "Directory not empty",
  34. ecodeExistingPeerAddr: "Peer address has existed",
  35. // Post form related errors
  36. ecodeValueRequired: "Value is Required in POST form",
  37. EcodePrevValueRequired: "PrevValue is Required in POST form",
  38. EcodeTTLNaN: "The given TTL in POST form is not a number",
  39. EcodeIndexNaN: "The given index in POST form is not a number",
  40. ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
  41. ecodeTimeoutNaN: "The given timeout in POST form is not a number",
  42. ecodeNameRequired: "Name is required in POST form",
  43. ecodeIndexOrValueRequired: "Index or value is required",
  44. ecodeIndexValueMutex: "Index and value cannot both be specified",
  45. EcodeInvalidField: "Invalid field",
  46. EcodeInvalidForm: "Invalid POST form",
  47. // raft related errors
  48. EcodeRaftInternal: "Raft Internal Error",
  49. EcodeLeaderElect: "During Leader Election",
  50. // etcd related errors
  51. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  52. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  53. ecodeStandbyInternal: "Standby Internal Error",
  54. ecodeInvalidActiveSize: "Invalid active size",
  55. ecodeInvalidRemoveDelay: "Standby remove delay",
  56. // client related errors
  57. ecodeClientInternal: "Client Internal Error",
  58. }
  59. var errorStatus = map[int]int{
  60. EcodeKeyNotFound: http.StatusNotFound,
  61. EcodeNotFile: http.StatusForbidden,
  62. EcodeDirNotEmpty: http.StatusForbidden,
  63. EcodeTestFailed: http.StatusPreconditionFailed,
  64. EcodeNodeExist: http.StatusPreconditionFailed,
  65. EcodeRaftInternal: http.StatusInternalServerError,
  66. EcodeLeaderElect: http.StatusInternalServerError,
  67. }
  68. const (
  69. EcodeKeyNotFound = 100
  70. EcodeTestFailed = 101
  71. EcodeNotFile = 102
  72. ecodeNoMorePeer = 103
  73. EcodeNotDir = 104
  74. EcodeNodeExist = 105
  75. ecodeKeyIsPreserved = 106
  76. EcodeRootROnly = 107
  77. EcodeDirNotEmpty = 108
  78. ecodeExistingPeerAddr = 109
  79. ecodeValueRequired = 200
  80. EcodePrevValueRequired = 201
  81. EcodeTTLNaN = 202
  82. EcodeIndexNaN = 203
  83. ecodeValueOrTTLRequired = 204
  84. ecodeTimeoutNaN = 205
  85. ecodeNameRequired = 206
  86. ecodeIndexOrValueRequired = 207
  87. ecodeIndexValueMutex = 208
  88. EcodeInvalidField = 209
  89. EcodeInvalidForm = 210
  90. EcodeRaftInternal = 300
  91. EcodeLeaderElect = 301
  92. EcodeWatcherCleared = 400
  93. EcodeEventIndexCleared = 401
  94. ecodeStandbyInternal = 402
  95. ecodeInvalidActiveSize = 403
  96. ecodeInvalidRemoveDelay = 404
  97. ecodeClientInternal = 500
  98. )
  99. type Error struct {
  100. ErrorCode int `json:"errorCode"`
  101. Message string `json:"message"`
  102. Cause string `json:"cause,omitempty"`
  103. Index uint64 `json:"index"`
  104. }
  105. func NewRequestError(errorCode int, cause string) *Error {
  106. return NewError(errorCode, cause, 0)
  107. }
  108. func NewError(errorCode int, cause string, index uint64) *Error {
  109. return &Error{
  110. ErrorCode: errorCode,
  111. Message: errors[errorCode],
  112. Cause: cause,
  113. Index: index,
  114. }
  115. }
  116. // Only for error interface
  117. func (e Error) Error() string {
  118. return e.Message + " (" + e.Cause + ")"
  119. }
  120. func (e Error) toJsonString() string {
  121. b, _ := json.Marshal(e)
  122. return string(b)
  123. }
  124. func (e Error) statusCode() int {
  125. status, ok := errorStatus[e.ErrorCode]
  126. if !ok {
  127. status = http.StatusBadRequest
  128. }
  129. return status
  130. }
  131. func (e Error) WriteTo(w http.ResponseWriter) {
  132. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  133. w.Header().Set("Content-Type", "application/json")
  134. w.WriteHeader(e.statusCode())
  135. fmt.Fprintln(w, e.toJsonString())
  136. }