From d03e992b4f3d384bdd61633bef42391df3fac48f Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Tue, 24 Jan 2017 16:35:48 +1300 Subject: [PATCH] feat(api): replace all calls to http.Error with custom Error writer --- api/http/docker_handler.go | 6 +++--- api/http/handler.go | 4 +++- api/http/middleware.go | 4 ++-- api/http/templates_handler.go | 7 ++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/http/docker_handler.go b/api/http/docker_handler.go index 4894b797c..9cffa4b26 100644 --- a/api/http/docker_handler.go +++ b/api/http/docker_handler.go @@ -135,7 +135,7 @@ type unixSocketHandler struct { func (h *unixSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { conn, err := net.Dial("unix", h.path) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + Error(w, err, http.StatusInternalServerError, nil) return } c := httputil.NewClientConn(conn, nil) @@ -143,7 +143,7 @@ func (h *unixSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { res, err := c.Do(r) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + Error(w, err, http.StatusInternalServerError, nil) return } defer res.Body.Close() @@ -154,6 +154,6 @@ func (h *unixSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } if _, err := io.Copy(w, res.Body); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + Error(w, err, http.StatusInternalServerError, nil) } } diff --git a/api/http/handler.go b/api/http/handler.go index 5c88a2805..efce098b5 100644 --- a/api/http/handler.go +++ b/api/http/handler.go @@ -55,7 +55,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Error writes an API error message to the response and logger. func Error(w http.ResponseWriter, err error, code int, logger *log.Logger) { // Log error. - logger.Printf("http error: %s (code=%d)", err, code) + if logger != nil { + logger.Printf("http error: %s (code=%d)", err, code) + } // Write generic error response. w.WriteHeader(code) diff --git a/api/http/middleware.go b/api/http/middleware.go index 99775dec6..ff38a736a 100644 --- a/api/http/middleware.go +++ b/api/http/middleware.go @@ -47,13 +47,13 @@ func (service *middleWareService) middleWareAuthenticate(next http.Handler) http } if token == "" { - http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + Error(w, portainer.ErrUnauthorized, http.StatusUnauthorized, nil) return } err := service.jwtService.VerifyToken(token) if err != nil { - http.Error(w, err.Error(), http.StatusUnauthorized) + Error(w, err, http.StatusUnauthorized, nil) return } diff --git a/api/http/templates_handler.go b/api/http/templates_handler.go index 867891291..a72a6a4c1 100644 --- a/api/http/templates_handler.go +++ b/api/http/templates_handler.go @@ -1,7 +1,6 @@ package http import ( - "fmt" "io/ioutil" "log" "net/http" @@ -40,15 +39,13 @@ func (handler *TemplatesHandler) handleGetTemplates(w http.ResponseWriter, r *ht resp, err := http.Get(handler.templatesURL) if err != nil { - log.Print(err) - http.Error(w, fmt.Sprintf("Error making request to %s: %s", handler.templatesURL, err.Error()), http.StatusInternalServerError) + Error(w, err, http.StatusInternalServerError, handler.Logger) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Print(err) - http.Error(w, "Error reading body from templates URL", http.StatusInternalServerError) + Error(w, err, http.StatusInternalServerError, handler.Logger) return } w.Header().Set("Content-Type", "application/json")