Log errors in response to http requests. (#1569)

This commit is contained in:
Tom Wilkie
2016-06-09 09:01:50 +01:00
parent bbf55922d0
commit 141ce75902
7 changed files with 21 additions and 14 deletions

View File

@@ -15,7 +15,7 @@ func makeRawReportHandler(rep Reporter) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
report, err := rep.Report(ctx)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
respondWith(w, http.StatusOK, report)
@@ -34,7 +34,7 @@ func makeProbeHandler(rep Reporter) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
rpt, err := rep.Report(ctx)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
result := []probeDesc{}

View File

@@ -278,7 +278,7 @@ func (r *registry) makeTopologyList(rep Reporter) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
report, err := rep.Report(ctx)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
respondWith(w, http.StatusOK, r.renderTopologies(report, req))
@@ -369,13 +369,13 @@ func (r *registry) captureRenderer(rep Reporter, f rendererHandler) CtxHandlerFu
}
rpt, err := rep.Report(ctx)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
req.ParseForm()
renderer, decorator, err := r.rendererForTopology(topologyID, req.Form, rpt)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
f(ctx, renderer, decorator, rpt, w, req)

View File

@@ -59,7 +59,7 @@ func handleWebsocket(
r *http.Request,
) {
if err := r.ParseForm(); err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
return
}
loop := websocketLoop

View File

@@ -75,7 +75,7 @@ func handleProbeWS(cr ControlRouter) CtxHandlerFunc {
return res
})
if err != nil {
respondWith(w, http.StatusBadRequest, err.Error())
respondWith(w, http.StatusBadRequest, err)
return
}
defer cr.Deregister(ctx, probeID, id)

View File

@@ -34,7 +34,7 @@ func checkPipe(pr PipeRouter, end End) CtxHandlerFunc {
id := mux.Vars(r)["pipeID"]
exists, err := pr.Exists(ctx, id)
if err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
} else if exists {
w.WriteHeader(http.StatusNoContent)
} else {
@@ -73,7 +73,7 @@ func deletePipe(pr PipeRouter) CtxHandlerFunc {
pipeID := mux.Vars(r)["pipeID"]
log.Infof("Deleting pipe %s", pipeID)
if err := pr.Delete(ctx, pipeID); err != nil {
respondWith(w, http.StatusInternalServerError, err.Error())
respondWith(w, http.StatusInternalServerError, err)
}
}
}

View File

@@ -134,7 +134,7 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
reader, err = gzip.NewReader(reader)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
respondWith(w, http.StatusBadRequest, err)
return
}
}
@@ -150,7 +150,7 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
}
if err := decoder(&rpt); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
respondWith(w, http.StatusBadRequest, err)
return
}
log.Debugf(
@@ -162,7 +162,7 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
if err := a.Add(ctx, rpt); err != nil {
log.Errorf("Error Adding report: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
respondWith(w, http.StatusInternalServerError, err)
return
}
w.WriteHeader(http.StatusOK)
@@ -188,7 +188,7 @@ func apiHandler(rep Reporter) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
report, err := rep.Report(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
respondWith(w, http.StatusInternalServerError, err)
return
}
newVersion.Lock()

View File

@@ -9,11 +9,18 @@ import (
)
func respondWith(w http.ResponseWriter, code int, response interface{}) {
if err, ok := response.(error); ok {
log.Errorf("Error %d: %v", code, err)
response = err.Error()
} else if 500 <= code && code < 600 {
log.Errorf("Non-error %d: %v", code, response)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(code)
encoder := codec.NewEncoder(w, &codec.JsonHandle{})
if err := encoder.Encode(response); err != nil {
log.Error(err)
log.Errorf("Error encdoing response: %v", err)
}
}