error.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. EcodeUnauthorized: "The request requires user authentication",
  36. // Post form related errors
  37. ecodeValueRequired: "Value is Required in POST form",
  38. EcodePrevValueRequired: "PrevValue is Required in POST form",
  39. EcodeTTLNaN: "The given TTL in POST form is not a number",
  40. EcodeIndexNaN: "The given index in POST form is not a number",
  41. ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
  42. ecodeTimeoutNaN: "The given timeout in POST form is not a number",
  43. ecodeNameRequired: "Name is required in POST form",
  44. ecodeIndexOrValueRequired: "Index or value is required",
  45. ecodeIndexValueMutex: "Index and value cannot both be specified",
  46. EcodeInvalidField: "Invalid field",
  47. EcodeInvalidForm: "Invalid POST form",
  48. // raft related errors
  49. EcodeRaftInternal: "Raft Internal Error",
  50. EcodeLeaderElect: "During Leader Election",
  51. // etcd related errors
  52. EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
  53. EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
  54. ecodeStandbyInternal: "Standby Internal Error",
  55. ecodeInvalidActiveSize: "Invalid active size",
  56. ecodeInvalidRemoveDelay: "Standby remove delay",
  57. // client related errors
  58. ecodeClientInternal: "Client Internal Error",
  59. }
  60. var errorStatus = map[int]int{
  61. EcodeKeyNotFound: http.StatusNotFound,
  62. EcodeNotFile: http.StatusForbidden,
  63. EcodeDirNotEmpty: http.StatusForbidden,
  64. EcodeUnauthorized: http.StatusUnauthorized,
  65. EcodeTestFailed: http.StatusPreconditionFailed,
  66. EcodeNodeExist: http.StatusPreconditionFailed,
  67. EcodeRaftInternal: http.StatusInternalServerError,
  68. EcodeLeaderElect: http.StatusInternalServerError,
  69. }
  70. const (
  71. EcodeKeyNotFound = 100
  72. EcodeTestFailed = 101
  73. EcodeNotFile = 102
  74. ecodeNoMorePeer = 103
  75. EcodeNotDir = 104
  76. EcodeNodeExist = 105
  77. ecodeKeyIsPreserved = 106
  78. EcodeRootROnly = 107
  79. EcodeDirNotEmpty = 108
  80. ecodeExistingPeerAddr = 109
  81. EcodeUnauthorized = 110
  82. ecodeValueRequired = 200
  83. EcodePrevValueRequired = 201
  84. EcodeTTLNaN = 202
  85. EcodeIndexNaN = 203
  86. ecodeValueOrTTLRequired = 204
  87. ecodeTimeoutNaN = 205
  88. ecodeNameRequired = 206
  89. ecodeIndexOrValueRequired = 207
  90. ecodeIndexValueMutex = 208
  91. EcodeInvalidField = 209
  92. EcodeInvalidForm = 210
  93. EcodeRaftInternal = 300
  94. EcodeLeaderElect = 301
  95. EcodeWatcherCleared = 400
  96. EcodeEventIndexCleared = 401
  97. ecodeStandbyInternal = 402
  98. ecodeInvalidActiveSize = 403
  99. ecodeInvalidRemoveDelay = 404
  100. ecodeClientInternal = 500
  101. )
  102. type Error struct {
  103. ErrorCode int `json:"errorCode"`
  104. Message string `json:"message"`
  105. Cause string `json:"cause,omitempty"`
  106. Index uint64 `json:"index"`
  107. }
  108. func NewRequestError(errorCode int, cause string) *Error {
  109. return NewError(errorCode, cause, 0)
  110. }
  111. func NewError(errorCode int, cause string, index uint64) *Error {
  112. return &Error{
  113. ErrorCode: errorCode,
  114. Message: errors[errorCode],
  115. Cause: cause,
  116. Index: index,
  117. }
  118. }
  119. // Only for error interface
  120. func (e Error) Error() string {
  121. return e.Message + " (" + e.Cause + ")"
  122. }
  123. func (e Error) toJsonString() string {
  124. b, _ := json.Marshal(e)
  125. return string(b)
  126. }
  127. func (e Error) statusCode() int {
  128. status, ok := errorStatus[e.ErrorCode]
  129. if !ok {
  130. status = http.StatusBadRequest
  131. }
  132. return status
  133. }
  134. func (e Error) WriteTo(w http.ResponseWriter) {
  135. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  136. w.Header().Set("Content-Type", "application/json")
  137. w.WriteHeader(e.statusCode())
  138. fmt.Fprintln(w, e.toJsonString())
  139. }