etcd/error/error.go

157 lines
4.8 KiB
Go
Raw Normal View History

// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-10-07 20:44:51 +04:00
2015-01-15 23:04:51 +03:00
// error package describes errors in etcd project.
// When any change happens, Documentation/errorcode.md needs to be updated
// correspondingly.
2013-08-18 07:41:15 +04:00
package error
2013-07-12 21:42:07 +04:00
import (
"encoding/json"
2013-10-06 22:23:52 +04:00
"fmt"
2013-08-18 07:41:15 +04:00
"net/http"
2013-07-12 21:42:07 +04:00
)
var errors = map[int]string{
// command related errors
EcodeKeyNotFound: "Key not found",
EcodeTestFailed: "Compare failed", //test and set
EcodeNotFile: "Not a file",
2015-01-15 22:49:41 +03:00
ecodeNoMorePeer: "Reached the max number of peers in the cluster",
EcodeNotDir: "Not a directory",
EcodeNodeExist: "Key already exists", // create
2015-01-15 22:49:41 +03:00
ecodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
2015-01-15 23:04:51 +03:00
EcodeRootROnly: "Root is read only",
EcodeDirNotEmpty: "Directory not empty",
2015-01-15 22:49:41 +03:00
ecodeExistingPeerAddr: "Peer address has existed",
// Post form related errors
2015-01-15 22:49:41 +03:00
ecodeValueRequired: "Value is Required in POST form",
EcodePrevValueRequired: "PrevValue is Required in POST form",
EcodeTTLNaN: "The given TTL in POST form is not a number",
EcodeIndexNaN: "The given index in POST form is not a number",
2015-01-15 22:49:41 +03:00
ecodeValueOrTTLRequired: "Value or TTL is required in POST form",
ecodeTimeoutNaN: "The given timeout in POST form is not a number",
ecodeNameRequired: "Name is required in POST form",
ecodeIndexOrValueRequired: "Index or value is required",
ecodeIndexValueMutex: "Index and value cannot both be specified",
EcodeInvalidField: "Invalid field",
2014-09-11 02:46:13 +04:00
EcodeInvalidForm: "Invalid POST form",
// raft related errors
EcodeRaftInternal: "Raft Internal Error",
EcodeLeaderElect: "During Leader Election",
// etcd related errors
EcodeWatcherCleared: "watcher is cleared due to etcd recovery",
EcodeEventIndexCleared: "The event in requested index is outdated and cleared",
2015-01-15 22:49:41 +03:00
ecodeStandbyInternal: "Standby Internal Error",
ecodeInvalidActiveSize: "Invalid active size",
ecodeInvalidRemoveDelay: "Standby remove delay",
// client related errors
2015-01-15 22:49:41 +03:00
ecodeClientInternal: "Client Internal Error",
}
2013-07-13 00:35:43 +04:00
var errorStatus = map[int]int{
EcodeKeyNotFound: http.StatusNotFound,
EcodeNotFile: http.StatusForbidden,
EcodeDirNotEmpty: http.StatusForbidden,
EcodeTestFailed: http.StatusPreconditionFailed,
EcodeNodeExist: http.StatusPreconditionFailed,
EcodeRaftInternal: http.StatusInternalServerError,
EcodeLeaderElect: http.StatusInternalServerError,
}
2013-09-09 02:46:16 +04:00
const (
EcodeKeyNotFound = 100
EcodeTestFailed = 101
EcodeNotFile = 102
2015-01-15 22:49:41 +03:00
ecodeNoMorePeer = 103
EcodeNotDir = 104
EcodeNodeExist = 105
2015-01-15 22:49:41 +03:00
ecodeKeyIsPreserved = 106
EcodeRootROnly = 107
EcodeDirNotEmpty = 108
2015-01-15 22:49:41 +03:00
ecodeExistingPeerAddr = 109
2013-09-09 02:46:16 +04:00
2015-01-15 22:49:41 +03:00
ecodeValueRequired = 200
EcodePrevValueRequired = 201
EcodeTTLNaN = 202
EcodeIndexNaN = 203
2015-01-15 22:49:41 +03:00
ecodeValueOrTTLRequired = 204
ecodeTimeoutNaN = 205
ecodeNameRequired = 206
ecodeIndexOrValueRequired = 207
ecodeIndexValueMutex = 208
EcodeInvalidField = 209
2014-09-11 02:46:13 +04:00
EcodeInvalidForm = 210
2013-09-09 02:46:16 +04:00
EcodeRaftInternal = 300
EcodeLeaderElect = 301
EcodeWatcherCleared = 400
EcodeEventIndexCleared = 401
2015-01-15 22:49:41 +03:00
ecodeStandbyInternal = 402
ecodeInvalidActiveSize = 403
ecodeInvalidRemoveDelay = 404
2015-01-15 22:49:41 +03:00
ecodeClientInternal = 500
2013-09-09 02:46:16 +04:00
)
2013-08-18 07:41:15 +04:00
type Error struct {
2013-07-13 00:35:43 +04:00
ErrorCode int `json:"errorCode"`
Message string `json:"message"`
Cause string `json:"cause,omitempty"`
2013-10-06 22:23:52 +04:00
Index uint64 `json:"index"`
2013-07-12 21:42:07 +04:00
}
2014-09-11 02:46:13 +04:00
func NewRequestError(errorCode int, cause string) *Error {
return NewError(errorCode, cause, 0)
}
func NewError(errorCode int, cause string, index uint64) *Error {
2013-10-06 22:23:52 +04:00
return &Error{
2013-07-13 00:35:43 +04:00
ErrorCode: errorCode,
Message: errors[errorCode],
Cause: cause,
2013-10-06 22:23:52 +04:00
Index: index,
2013-08-18 02:06:21 +04:00
}
}
2013-08-18 07:41:15 +04:00
// Only for error interface
func (e Error) Error() string {
return e.Message + " (" + e.Cause + ")"
2013-08-18 07:41:15 +04:00
}
func (e Error) toJsonString() string {
2013-08-18 02:06:21 +04:00
b, _ := json.Marshal(e)
2013-08-18 07:41:15 +04:00
return string(b)
}
func (e Error) statusCode() int {
status, ok := errorStatus[e.ErrorCode]
if !ok {
status = http.StatusBadRequest
}
return status
}
2014-10-28 01:32:36 +03:00
func (e Error) WriteTo(w http.ResponseWriter) {
2013-10-06 22:23:52 +04:00
w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(e.statusCode())
fmt.Fprintln(w, e.toJsonString())
2013-07-12 21:42:07 +04:00
}