clean error handling

release-0.4
Xiang Li 2013-08-17 15:06:21 -07:00
parent ef4aef950e
commit 8ed67bedbb
3 changed files with 91 additions and 113 deletions

View File

@ -33,17 +33,21 @@ func init() {
} }
type jsonError struct { type etcdError struct {
ErrorCode int `json:"errorCode"` ErrorCode int `json:"errorCode"`
Message string `json:"message"` Message string `json:"message"`
Cause string `json:"cause,omitempty"` Cause string `json:"cause,omitempty"`
} }
func newJsonError(errorCode int, cause string) []byte { func newEtcdError(errorCode int, cause string) *etcdError {
b, _ := json.Marshal(jsonError{ return &etcdError{
ErrorCode: errorCode, ErrorCode: errorCode,
Message: errors[errorCode], Message: errors[errorCode],
Cause: cause, Cause: cause,
}) }
}
func (e *etcdError) toJson() []byte {
b, _ := json.Marshal(e)
return b return b
} }

View File

@ -16,29 +16,42 @@ import (
func NewEtcdMuxer() *http.ServeMux { func NewEtcdMuxer() *http.ServeMux {
// external commands // external commands
etcdMux := http.NewServeMux() etcdMux := http.NewServeMux()
etcdMux.HandleFunc("/"+version+"/keys/", Multiplexer) etcdMux.Handle("/"+version+"/keys/", etcdHandler(Multiplexer))
etcdMux.HandleFunc("/"+version+"/watch/", WatchHttpHandler) etcdMux.Handle("/"+version+"/watch/", etcdHandler(WatchHttpHandler))
etcdMux.HandleFunc("/leader", LeaderHttpHandler) etcdMux.Handle("/leader", etcdHandler(LeaderHttpHandler))
etcdMux.HandleFunc("/machines", MachinesHttpHandler) etcdMux.Handle("/machines", etcdHandler(MachinesHttpHandler))
etcdMux.HandleFunc("/version", VersionHttpHandler) etcdMux.Handle("/version", etcdHandler(VersionHttpHandler))
etcdMux.HandleFunc("/stats", StatsHttpHandler) etcdMux.Handle("/stats", etcdHandler(StatsHttpHandler))
etcdMux.HandleFunc("/test/", TestHttpHandler) etcdMux.HandleFunc("/test/", TestHttpHandler)
return etcdMux return etcdMux
} }
type etcdHandler func(http.ResponseWriter, *http.Request) *etcdError
func (fn etcdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e := fn(w, r); e != nil {
// 3xx is reft internal error
if e.ErrorCode/100 == 3 {
http.Error(w, string(e.toJson()), http.StatusInternalServerError)
} else {
http.Error(w, string(e.toJson()), http.StatusBadRequest)
}
}
}
// Multiplex GET/POST/DELETE request to corresponding handlers // Multiplex GET/POST/DELETE request to corresponding handlers
func Multiplexer(w http.ResponseWriter, req *http.Request) { func Multiplexer(w http.ResponseWriter, req *http.Request) *etcdError {
switch req.Method { switch req.Method {
case "GET": case "GET":
GetHttpHandler(&w, req) return GetHttpHandler(w, req)
case "POST": case "POST":
SetHttpHandler(&w, req) return SetHttpHandler(w, req)
case "DELETE": case "DELETE":
DeleteHttpHandler(&w, req) return DeleteHttpHandler(w, req)
default: default:
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return return nil
} }
} }
@ -48,15 +61,11 @@ func Multiplexer(w http.ResponseWriter, req *http.Request) {
//-------------------------------------- //--------------------------------------
// Set Command Handler // Set Command Handler
func SetHttpHandler(w *http.ResponseWriter, req *http.Request) { func SetHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
key := req.URL.Path[len("/v1/keys/"):] key := req.URL.Path[len("/v1/keys/"):]
if store.CheckKeyword(key) { if store.CheckKeyword(key) {
return newEtcdError(400, "Set")
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(400, "Set"))
return
} }
debugf("[recv] POST %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr) debugf("[recv] POST %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
@ -64,10 +73,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
value := req.FormValue("value") value := req.FormValue("value")
if len(value) == 0 { if len(value) == 0 {
(*w).WriteHeader(http.StatusBadRequest) return newEtcdError(200, "Set")
(*w).Write(newJsonError(200, "Set"))
return
} }
prevValue := req.FormValue("prevValue") prevValue := req.FormValue("prevValue")
@ -77,11 +83,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
expireTime, err := durationToExpireTime(strDuration) expireTime, err := durationToExpireTime(strDuration)
if err != nil { if err != nil {
return newEtcdError(202, "Set")
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(202, "Set"))
return
} }
if len(prevValue) != 0 { if len(prevValue) != 0 {
@ -92,7 +94,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
ExpireTime: expireTime, ExpireTime: expireTime,
} }
dispatch(command, w, req, true) return dispatch(command, w, req, true)
} else { } else {
command := &SetCommand{ command := &SetCommand{
@ -101,13 +103,12 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
ExpireTime: expireTime, ExpireTime: expireTime,
} }
dispatch(command, w, req, true) return dispatch(command, w, req, true)
} }
} }
// Delete Handler // Delete Handler
func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) { func DeleteHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
key := req.URL.Path[len("/v1/keys/"):] key := req.URL.Path[len("/v1/keys/"):]
debugf("[recv] DELETE %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr) debugf("[recv] DELETE %v/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
@ -116,76 +117,61 @@ func DeleteHttpHandler(w *http.ResponseWriter, req *http.Request) {
Key: key, Key: key,
} }
dispatch(command, w, req, true) return dispatch(command, w, req, true)
} }
// Dispatch the command to leader // Dispatch the command to leader
func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) { func dispatch(c Command, w http.ResponseWriter, req *http.Request, etcd bool) *etcdError {
if r.State() == raft.Leader { if r.State() == raft.Leader {
if body, err := r.Do(c); err != nil { if body, err := r.Do(c); err != nil {
// store error
if _, ok := err.(store.NotFoundError); ok { if _, ok := err.(store.NotFoundError); ok {
(*w).WriteHeader(http.StatusNotFound) return newEtcdError(100, err.Error())
(*w).Write(newJsonError(100, err.Error()))
return
} }
if _, ok := err.(store.TestFail); ok { if _, ok := err.(store.TestFail); ok {
(*w).WriteHeader(http.StatusBadRequest) return newEtcdError(101, err.Error())
(*w).Write(newJsonError(101, err.Error()))
return
} }
if _, ok := err.(store.NotFile); ok { if _, ok := err.(store.NotFile); ok {
(*w).WriteHeader(http.StatusBadRequest) return newEtcdError(102, err.Error())
(*w).Write(newJsonError(102, err.Error()))
return
} }
if err.Error() == errors[103] {
(*w).WriteHeader(http.StatusBadRequest)
(*w).Write(newJsonError(103, ""))
return
}
(*w).WriteHeader(http.StatusInternalServerError)
(*w).Write(newJsonError(300, err.Error()))
return
} else {
if body == nil { // join error
(*w).WriteHeader(http.StatusNotFound) if err.Error() == errors[103] {
(*w).Write(newJsonError(300, "Empty result from raft")) return newEtcdError(103, "")
} else { }
body, ok := body.([]byte)
// this should not happen // raft internal error
if !ok { return newEtcdError(300, err.Error())
panic("wrong type")
} } else {
(*w).WriteHeader(http.StatusOK) if body == nil {
(*w).Write(body) return newEtcdError(300, "Empty result from raft")
} else {
body, _ := body.([]byte)
w.WriteHeader(http.StatusOK)
w.Write(body)
return nil
} }
return
} }
} else { } else {
leader := r.Leader() leader := r.Leader()
// current no leader // current no leader
if leader == "" { if leader == "" {
(*w).WriteHeader(http.StatusInternalServerError) return newEtcdError(300, "")
(*w).Write(newJsonError(300, ""))
return
} }
// tell the client where is the leader // tell the client where is the leader
path := req.URL.Path path := req.URL.Path
var url string var url string
if etcd { if etcd {
etcdAddr, _ := nameToEtcdURL(leader) etcdAddr, _ := nameToEtcdURL(leader)
if etcdAddr == "" {
panic(leader)
}
url = etcdAddr + path url = etcdAddr + path
} else { } else {
raftAddr, _ := nameToRaftURL(leader) raftAddr, _ := nameToRaftURL(leader)
@ -194,12 +180,10 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) {
debugf("Redirect to %s", url) debugf("Redirect to %s", url)
http.Redirect(*w, req, url, http.StatusTemporaryRedirect) http.Redirect(w, req, url, http.StatusTemporaryRedirect)
return return nil
} }
(*w).WriteHeader(http.StatusInternalServerError) return newEtcdError(300, "")
(*w).Write(newJsonError(300, ""))
return
} }
//-------------------------------------- //--------------------------------------
@ -210,44 +194,45 @@ func dispatch(c Command, w *http.ResponseWriter, req *http.Request, etcd bool) {
//-------------------------------------- //--------------------------------------
// Handler to return the current leader's raft address // Handler to return the current leader's raft address
func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) { func LeaderHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
leader := r.Leader() leader := r.Leader()
if leader != "" { if leader != "" {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
raftURL, _ := nameToRaftURL(leader) raftURL, _ := nameToRaftURL(leader)
w.Write([]byte(raftURL)) w.Write([]byte(raftURL))
return nil
} else { } else {
return newEtcdError(301, "")
// not likely, but it may happen
w.WriteHeader(http.StatusInternalServerError)
w.Write(newJsonError(301, ""))
} }
} }
// Handler to return all the known machines in the current cluster // Handler to return all the known machines in the current cluster
func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) { func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
machines := getMachines() machines := getMachines()
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Join(machines, ", "))) w.Write([]byte(strings.Join(machines, ", ")))
return nil
} }
// Handler to return the current version of etcd // Handler to return the current version of etcd
func VersionHttpHandler(w http.ResponseWriter, req *http.Request) { func VersionHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("etcd %s", releaseVersion))) fmt.Fprintf(w, "etcd %s", releaseVersion)
w.Write([]byte(fmt.Sprintf("etcd API %s", version))) fmt.Fprintf(w, "etcd API %s", version)
return nil
} }
// Handler to return the basic stats of etcd // Handler to return the basic stats of etcd
func StatsHttpHandler(w http.ResponseWriter, req *http.Request) { func StatsHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(etcdStore.Stats()) w.Write(etcdStore.Stats())
return nil
} }
// Get Handler // Get Handler
func GetHttpHandler(w *http.ResponseWriter, req *http.Request) { func GetHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
key := req.URL.Path[len("/v1/keys/"):] key := req.URL.Path[len("/v1/keys/"):]
debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr) debugf("[recv] GET %s/v1/keys/%s [%s]", e.url, key, req.RemoteAddr)
@ -259,29 +244,23 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
if body, err := command.Apply(r.Server); err != nil { if body, err := command.Apply(r.Server); err != nil {
if _, ok := err.(store.NotFoundError); ok { if _, ok := err.(store.NotFoundError); ok {
(*w).WriteHeader(http.StatusNotFound) return newEtcdError(100, err.Error())
(*w).Write(newJsonError(100, err.Error()))
return
} }
(*w).WriteHeader(http.StatusInternalServerError) return newEtcdError(300, "")
(*w).Write(newJsonError(300, ""))
} else { } else {
body, ok := body.([]byte) body, _ := body.([]byte)
if !ok { w.WriteHeader(http.StatusOK)
panic("wrong type") w.Write(body)
}
(*w).WriteHeader(http.StatusOK)
(*w).Write(body)
return nil
} }
} }
// Watch handler // Watch handler
func WatchHttpHandler(w http.ResponseWriter, req *http.Request) { func WatchHttpHandler(w http.ResponseWriter, req *http.Request) *etcdError {
key := req.URL.Path[len("/v1/watch/"):] key := req.URL.Path[len("/v1/watch/"):]
command := &WatchCommand{ command := &WatchCommand{
@ -300,28 +279,23 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
sinceIndex, err := strconv.ParseUint(string(content), 10, 64) sinceIndex, err := strconv.ParseUint(string(content), 10, 64)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) return newEtcdError(203, "Watch From Index")
w.Write(newJsonError(203, "Watch From Index"))
} }
command.SinceIndex = sinceIndex command.SinceIndex = sinceIndex
} else { } else {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return return nil
} }
if body, err := command.Apply(r.Server); err != nil { if body, err := command.Apply(r.Server); err != nil {
w.WriteHeader(http.StatusInternalServerError) return newEtcdError(500, key)
w.Write(newJsonError(500, key))
} else { } else {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
body, ok := body.([]byte) body, _ := body.([]byte)
if !ok {
panic("wrong type")
}
w.Write(body) w.Write(body)
return nil
} }
} }

View File

@ -100,7 +100,7 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
if err := decodeJsonRequest(req, command); err == nil { if err := decodeJsonRequest(req, command); err == nil {
debugf("Receive Join Request from %s", command.Name) debugf("Receive Join Request from %s", command.Name)
dispatch(command, &w, req, false) dispatch(command, w, req, false)
} else { } else {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return