From c9a6a5c629df47980b4293a49bcb5c380ab4f162 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Wed, 15 Feb 2017 17:24:58 +0300 Subject: [PATCH] Errors to string --- rest/api.go | 23 +++++++++++++++++++---- rest/zoo.go | 10 +++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/rest/api.go b/rest/api.go index 6400093..0bb0e7c 100644 --- a/rest/api.go +++ b/rest/api.go @@ -13,14 +13,14 @@ type Ls struct { Childrens []string `json:"childrens"` Path string `json:"path"` State string `json:"state"` - Error error `json:"error"` + Error string `json:"error"` } // Get ... type Get struct { Path string `json:"path"` State string `json:"state"` - Error error `json:"error"` + Error string `json:"error"` Data []byte `json:"data"` } @@ -34,11 +34,18 @@ func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) { go func() { ch <- zk.GetChildrens(path) }() childrens := <-ch - data, err := json.Marshal(childrens) if err != nil { w.WriteHeader(500) + w.Write([]byte("JSON parsing failure")) + return } + if childrens.Error != "" { + w.WriteHeader(500) + w.Write(data) + return + } + w.WriteHeader(200) w.Write(data) } @@ -53,11 +60,19 @@ func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) { go func() { ch <- zk.GetNode(path) }() childrens := <-ch - data, err := json.Marshal(childrens) if err != nil { w.WriteHeader(500) + w.Write([]byte("JSON parsing failure")) + return } + + if childrens.Error != "" { + w.WriteHeader(500) + w.Write(data) + return + } + w.WriteHeader(200) w.Write(data) } diff --git a/rest/zoo.go b/rest/zoo.go index 0e68d2e..dad9b35 100644 --- a/rest/zoo.go +++ b/rest/zoo.go @@ -51,11 +51,11 @@ func (z ZooNode) GetChildrens(path string) Ls { childrens, _, err := z.Conn.Children(lsPath) if err != nil { l.State = "ERROR" - l.Error = err + l.Error = err.Error() return l } - l.Error = nil + l.Error = "" l.Childrens = childrens l.Path = lsPath @@ -74,7 +74,7 @@ func (z ZooNode) GetNode(path string) Get { getPath = strings.Replace(getPath, "//", "/", 1) } - log.Print("ls: ", getPath) + log.Print("get: ", getPath) var g Get g.State = "OK" @@ -82,11 +82,11 @@ func (z ZooNode) GetNode(path string) Get { data, _, err := z.Conn.Get(getPath) if err != nil { g.State = "ERROR" - g.Error = err + g.Error = err.Error() return g } - g.Error = nil + g.Error = "" g.Data = data g.Path = getPath