mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
create error map
This commit is contained in:
@@ -45,7 +45,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||
if len(command.Value) == 0 {
|
||||
(*w).WriteHeader(http.StatusBadRequest)
|
||||
|
||||
(*w).Write(newJsonError("400", "Value is Required"))
|
||||
(*w).Write(newJsonError(200, ""))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||
|
||||
(*w).WriteHeader(http.StatusBadRequest)
|
||||
|
||||
(*w).Write(newJsonError("400", "The given TTL is not a number"))
|
||||
(*w).Write(newJsonError(202, ""))
|
||||
}
|
||||
|
||||
dispatch(command, w, req, true)
|
||||
@@ -81,7 +81,7 @@ func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if len(command.Value) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
w.Write(newJsonError("400", "TestAndSet: Value Required"))
|
||||
w.Write(newJsonError(200, "TestAndSet"))
|
||||
|
||||
return
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if len(command.PrevValue) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
w.Write(newJsonError("400", "TestAndSet: PrevValue Required"))
|
||||
w.Write(newJsonError(201, "TestAndSet"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func TestAndSetHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
w.Write(newJsonError("400", "The given TTL is not a number"))
|
||||
w.Write(newJsonError(202, "TestAndSet"))
|
||||
}
|
||||
|
||||
dispatch(command, &w, req, true)
|
||||
@@ -132,11 +132,11 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool)
|
||||
|
||||
if _, ok := err.(store.TestFail); ok {
|
||||
(*w).WriteHeader(http.StatusBadRequest)
|
||||
(*w).Write(newJsonError("400", err.Error()))
|
||||
(*w).Write(newJsonError(101, err.Error()))
|
||||
return
|
||||
}
|
||||
(*w).WriteHeader(http.StatusInternalServerError)
|
||||
(*w).Write(newJsonError("500", "Internal Error"))
|
||||
(*w).Write(newJsonError(300, ""))
|
||||
return
|
||||
} else {
|
||||
|
||||
@@ -157,7 +157,7 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool)
|
||||
// current no leader
|
||||
if raftServer.Leader() == "" {
|
||||
(*w).WriteHeader(http.StatusInternalServerError)
|
||||
(*w).Write(newJsonError("500", "During Leader Election Period"))
|
||||
(*w).Write(newJsonError(300, ""))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, client bool)
|
||||
return
|
||||
}
|
||||
(*w).WriteHeader(http.StatusInternalServerError)
|
||||
(*w).Write(newJsonError("500", "Internal Error"))
|
||||
(*w).Write(newJsonError(300, ""))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
(*w).WriteHeader(http.StatusInternalServerError)
|
||||
(*w).Write(newJsonError("500", "Internal Error"))
|
||||
(*w).Write(newJsonError(300, ""))
|
||||
return
|
||||
} else {
|
||||
body, ok := body.([]byte)
|
||||
@@ -250,7 +250,7 @@ func ListHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write(newJsonError("500", "Internal Error"))
|
||||
w.Write(newJsonError(300, ""))
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -286,7 +286,7 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
|
||||
sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write(newJsonError("400", "Watch From Index: Vaild Index Required"))
|
||||
w.Write(newJsonError(203, "Watch From Index"))
|
||||
}
|
||||
command.SinceIndex = sinceIndex
|
||||
|
||||
|
||||
31
error.go
31
error.go
@@ -4,15 +4,34 @@ import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type jsonError struct {
|
||||
StatusCode string `json:"statusCode"`
|
||||
Message string `json:"message"`
|
||||
var errors map[int]string
|
||||
|
||||
func init() {
|
||||
errors = make(map[int]string)
|
||||
|
||||
// command related errors
|
||||
errors[100] = "Key Not Found"
|
||||
errors[101] = "The given PrevValue is not equal to the value of the key"
|
||||
// Post form related errors
|
||||
errors[200] = "Value is Required in POST form"
|
||||
errors[201] = "PrevValue is Required in POST form"
|
||||
errors[202] = "The given TTL in POST form is not a number"
|
||||
errors[203] = "The given index in POST form is not a number"
|
||||
// raft related errors
|
||||
errors[300] = "Raft Internal Error"
|
||||
}
|
||||
|
||||
func newJsonError(statusCode string, message string) []byte {
|
||||
type jsonError struct {
|
||||
ErrorCode int `json:"errorCode"`
|
||||
Message string `json:"message"`
|
||||
Cause string `json:"cause,omitempty"`
|
||||
}
|
||||
|
||||
func newJsonError(errorCode int, cause string) []byte {
|
||||
b, _ := json.Marshal(jsonError{
|
||||
StatusCode: statusCode,
|
||||
Message: message,
|
||||
ErrorCode: errorCode,
|
||||
Message: errors[errorCode],
|
||||
Cause: cause,
|
||||
})
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type NotFoundError string
|
||||
|
||||
func (e NotFoundError) Error() string {
|
||||
return fmt.Sprintf("Key %s Not Found", string(e))
|
||||
return string(e)
|
||||
}
|
||||
|
||||
type NotFile string
|
||||
|
||||
func (e NotFile) Error() string {
|
||||
return fmt.Sprintf("Try to set value to a dir %s", string(e))
|
||||
return string(e)
|
||||
}
|
||||
|
||||
type TestFail string
|
||||
|
||||
func (e TestFail) Error() string {
|
||||
return fmt.Sprintf("Test %s fails", string(e))
|
||||
return string(e)
|
||||
}
|
||||
@@ -376,7 +376,7 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim
|
||||
} else {
|
||||
|
||||
// If fails, return err
|
||||
err := TestFail(fmt.Sprintf("%s==%s", resp.Value, prevValue))
|
||||
err := TestFail(fmt.Sprintf("TestAndSet: %s!=%s", resp.Value, prevValue))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user