Errors to string

This commit is contained in:
Denis Zheleztsov 2017-02-15 17:24:58 +03:00
parent a141ddfcb3
commit c9a6a5c629
2 changed files with 24 additions and 9 deletions

View File

@ -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)
}

View File

@ -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