error.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. When any change happens,
  15. // Documentation/errorcode.md needs to be updated correspondingly.
  16. package error
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/http"
  21. )
  22. var errors = map[int]string{
  23. // command related errors
  24. EcodeKeyNotFound: "Key not found",
  25. EcodeTestFailed: "Compare failed", //test and set
  26. EcodeNotFile: "Not a file",
  27. ecodeNoMorePeer: "Reached the max number of peers in the cluster",
  28. EcodeNotDir: "Not a directory",
  29. EcodeNodeExist: "Key already exists", // create
  30. ecodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
  31. EcodeRootROnly: "Root is read only",
  32. EcodeDirNotEmpty: "Directory not empty",
  33. ecodeExistingPeerAddr: "Peer address has existed",
  34. EcodeUnauthorized: "The request requires user authentication",
  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. EcodeUnauthorized: http.StatusUnauthorized,
  64. EcodeTestFailed: http.StatusPreconditionFailed,
  65. EcodeNodeExist: http.StatusPreconditionFailed,
  66. EcodeRaftInternal: http.StatusInternalServerError,
  67. EcodeLeaderElect: http.StatusInternalServerError,
  68. }
  69. const (
  70. EcodeKeyNotFound = 100
  71. EcodeTestFailed = 101
  72. EcodeNotFile = 102
  73. ecodeNoMorePeer = 103
  74. EcodeNotDir = 104
  75. EcodeNodeExist = 105
  76. ecodeKeyIsPreserved = 106
  77. EcodeRootROnly = 107
  78. EcodeDirNotEmpty = 108
  79. ecodeExistingPeerAddr = 109
  80. EcodeUnauthorized = 110
  81. ecodeValueRequired = 200
  82. EcodePrevValueRequired = 201
  83. EcodeTTLNaN = 202
  84. EcodeIndexNaN = 203
  85. ecodeValueOrTTLRequired = 204
  86. ecodeTimeoutNaN = 205
  87. ecodeNameRequired = 206
  88. ecodeIndexOrValueRequired = 207
  89. ecodeIndexValueMutex = 208
  90. EcodeInvalidField = 209
  91. EcodeInvalidForm = 210
  92. EcodeRaftInternal = 300
  93. EcodeLeaderElect = 301
  94. EcodeWatcherCleared = 400
  95. EcodeEventIndexCleared = 401
  96. ecodeStandbyInternal = 402
  97. ecodeInvalidActiveSize = 403
  98. ecodeInvalidRemoveDelay = 404
  99. ecodeClientInternal = 500
  100. )
  101. type Error struct {
  102. ErrorCode int `json:"errorCode"`
  103. Message string `json:"message"`
  104. Cause string `json:"cause,omitempty"`
  105. Index uint64 `json:"index"`
  106. }
  107. func NewRequestError(errorCode int, cause string) *Error {
  108. return NewError(errorCode, cause, 0)
  109. }
  110. func NewError(errorCode int, cause string, index uint64) *Error {
  111. return &Error{
  112. ErrorCode: errorCode,
  113. Message: errors[errorCode],
  114. Cause: cause,
  115. Index: index,
  116. }
  117. }
  118. // Only for error interface
  119. func (e Error) Error() string {
  120. return e.Message + " (" + e.Cause + ")"
  121. }
  122. func (e Error) toJsonString() string {
  123. b, _ := json.Marshal(e)
  124. return string(b)
  125. }
  126. func (e Error) StatusCode() int {
  127. status, ok := errorStatus[e.ErrorCode]
  128. if !ok {
  129. status = http.StatusBadRequest
  130. }
  131. return status
  132. }
  133. func (e Error) WriteTo(w http.ResponseWriter) {
  134. w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
  135. w.Header().Set("Content-Type", "application/json")
  136. w.WriteHeader(e.StatusCode())
  137. fmt.Fprintln(w, e.toJsonString())
  138. }