use http package for status codes

This commit is contained in:
Steffen Vogel 2021-07-16 11:55:21 +02:00
parent cfb680e99a
commit ae4e4173c3

View File

@ -34,12 +34,12 @@ type graphHandler struct {
func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ns, err := h.mesh.Nodes().List() ns, err := h.mesh.Nodes().List()
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to list nodes: %v", err), 500) http.Error(w, fmt.Sprintf("failed to list nodes: %v", err), http.StatusInternalServerError)
return return
} }
ps, err := h.mesh.Peers().List() ps, err := h.mesh.Peers().List()
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to list peers: %v", err), 500) http.Error(w, fmt.Sprintf("failed to list peers: %v", err), http.StatusInternalServerError)
return return
} }
@ -57,7 +57,7 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
subnet.IP = subnet.IP.Mask(subnet.Mask) subnet.IP = subnet.IP.Mask(subnet.Mask)
if len(nodes) == 0 { if len(nodes) == 0 {
http.Error(w, "did not find any valid Kilo nodes in the cluster", 500) http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError)
return return
} }
peers := make(map[string]*mesh.Peer) peers := make(map[string]*mesh.Peer)
@ -68,13 +68,13 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
topo, err := mesh.NewTopology(nodes, peers, h.granularity, hostname, 0, []byte{}, subnet, nodes[hostname].PersistentKeepalive, nil) topo, err := mesh.NewTopology(nodes, peers, h.granularity, hostname, 0, []byte{}, subnet, nodes[hostname].PersistentKeepalive, nil)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to create topology: %v", err), 500) http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError)
return return
} }
dot, err := topo.Dot() dot, err := topo.Dot()
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), 500) http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), http.StatusInternalServerError)
} }
buf := bytes.NewBufferString(dot) buf := bytes.NewBufferString(dot)
@ -94,25 +94,25 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
stdin, err := command.StdinPipe() stdin, err := command.StdinPipe()
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
_, err = io.Copy(stdin, buf) _, err = io.Copy(stdin, buf)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
err = stdin.Close() err = stdin.Close()
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
output, err := command.Output() output, err := command.Output()
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("unable to execute dot: %v (is graphviz package installed?)", err), 500) http.Error(w, fmt.Sprintf("unable to execute dot: %v (is graphviz package installed?)", err), http.StatusInternalServerError)
return return
} }