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"` Childrens []string `json:"childrens"`
Path string `json:"path"` Path string `json:"path"`
State string `json:"state"` State string `json:"state"`
Error error `json:"error"` Error string `json:"error"`
} }
// Get ... // Get ...
type Get struct { type Get struct {
Path string `json:"path"` Path string `json:"path"`
State string `json:"state"` State string `json:"state"`
Error error `json:"error"` Error string `json:"error"`
Data []byte `json:"data"` Data []byte `json:"data"`
} }
@ -34,11 +34,18 @@ func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) {
go func() { ch <- zk.GetChildrens(path) }() go func() { ch <- zk.GetChildrens(path) }()
childrens := <-ch childrens := <-ch
data, err := json.Marshal(childrens) data, err := json.Marshal(childrens)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte("JSON parsing failure"))
return
} }
if childrens.Error != "" {
w.WriteHeader(500)
w.Write(data)
return
}
w.WriteHeader(200) w.WriteHeader(200)
w.Write(data) w.Write(data)
} }
@ -53,11 +60,19 @@ func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
go func() { ch <- zk.GetNode(path) }() go func() { ch <- zk.GetNode(path) }()
childrens := <-ch childrens := <-ch
data, err := json.Marshal(childrens) data, err := json.Marshal(childrens)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte("JSON parsing failure"))
return
} }
if childrens.Error != "" {
w.WriteHeader(500)
w.Write(data)
return
}
w.WriteHeader(200) w.WriteHeader(200)
w.Write(data) w.Write(data)
} }

View File

@ -51,11 +51,11 @@ func (z ZooNode) GetChildrens(path string) Ls {
childrens, _, err := z.Conn.Children(lsPath) childrens, _, err := z.Conn.Children(lsPath)
if err != nil { if err != nil {
l.State = "ERROR" l.State = "ERROR"
l.Error = err l.Error = err.Error()
return l return l
} }
l.Error = nil l.Error = ""
l.Childrens = childrens l.Childrens = childrens
l.Path = lsPath l.Path = lsPath
@ -74,7 +74,7 @@ func (z ZooNode) GetNode(path string) Get {
getPath = strings.Replace(getPath, "//", "/", 1) getPath = strings.Replace(getPath, "//", "/", 1)
} }
log.Print("ls: ", getPath) log.Print("get: ", getPath)
var g Get var g Get
g.State = "OK" g.State = "OK"
@ -82,11 +82,11 @@ func (z ZooNode) GetNode(path string) Get {
data, _, err := z.Conn.Get(getPath) data, _, err := z.Conn.Get(getPath)
if err != nil { if err != nil {
g.State = "ERROR" g.State = "ERROR"
g.Error = err g.Error = err.Error()
return g return g
} }
g.Error = nil g.Error = ""
g.Data = data g.Data = data
g.Path = getPath g.Path = getPath