From 0aa70e125cd2c92c513aef2dc7097ff704d81cf8 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 12 Jul 2013 13:35:43 -0700 Subject: [PATCH] create error map --- client_handlers.go | 24 ++++++++++++------------ error.go | 31 +++++++++++++++++++++++++------ store/error.go | 10 +++------- store/store.go | 2 +- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/client_handlers.go b/client_handlers.go index 81a0b0ed1..c151b05d9 100644 --- a/client_handlers.go +++ b/client_handlers.go @@ -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 diff --git a/error.go b/error.go index f4b46bdbb..4de0c7494 100644 --- a/error.go +++ b/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 } diff --git a/store/error.go b/store/error.go index 32ac4204d..3f13f83a5 100644 --- a/store/error.go +++ b/store/error.go @@ -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) } \ No newline at end of file diff --git a/store/store.go b/store/store.go index a50e4d68b..5289208fe 100644 --- a/store/store.go +++ b/store/store.go @@ -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 }