Rename package srverrors to httperror

- Package names are preferred to be in singular form¹.
- Since errors are just for HTTP and not generic, I changed the base name to
'http'.
- I didn't name it simply 'error', because it would then conflict with the
- standard 'error' interface.

¹: https://rakyll.org/style-packages/
This commit is contained in:
pancho horrillo
2019-11-20 07:49:38 +01:00
parent 0d66fe9963
commit 206aac5747
9 changed files with 77 additions and 77 deletions
+2 -2
View File
@@ -19,8 +19,8 @@ package data
import (
"net/http"
"github.com/BBVA/kapow/internal/server/httperror"
"github.com/BBVA/kapow/internal/server/model"
"github.com/BBVA/kapow/internal/server/srverrors"
"github.com/gorilla/mux"
)
@@ -40,7 +40,7 @@ func checkHandler(fn resourceHandler) func(http.ResponseWriter, *http.Request) {
if h, ok := Handlers.Get(handlerID); ok {
fn(w, r, h)
} else {
srverrors.ErrorJSON(w, "Handler ID Not Found", http.StatusNotFound)
httperror.ErrorJSON(w, "Handler ID Not Found", http.StatusNotFound)
}
}
}
+16 -16
View File
@@ -23,8 +23,8 @@ import (
"net/textproto"
"strconv"
"github.com/BBVA/kapow/internal/server/httperror"
"github.com/BBVA/kapow/internal/server/model"
"github.com/BBVA/kapow/internal/server/srverrors"
"github.com/gorilla/mux"
)
@@ -40,7 +40,7 @@ func getRequestBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
n, err := io.Copy(w, h.Request.Body)
if err != nil {
if n == 0 {
srverrors.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} else {
// Only way to abort current connection as of go 1.13
// https://github.com/golang/go/issues/16542
@@ -72,7 +72,7 @@ func getRequestMatches(w http.ResponseWriter, r *http.Request, h *model.Handler)
if value, ok := vars[name]; ok {
_, _ = w.Write([]byte(value))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -82,7 +82,7 @@ func getRequestParams(w http.ResponseWriter, r *http.Request, h *model.Handler)
if values, ok := h.Request.URL.Query()[name]; ok {
_, _ = w.Write([]byte(values[0]))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -92,7 +92,7 @@ func getRequestHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler)
if values, ok := h.Request.Header[textproto.CanonicalMIMEHeaderKey(name)]; ok {
_, _ = w.Write([]byte(values[0]))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -102,7 +102,7 @@ func getRequestCookies(w http.ResponseWriter, r *http.Request, h *model.Handler)
if cookie, err := h.Request.Cookie(name); err == nil {
_, _ = w.Write([]byte(cookie.Value))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -118,11 +118,11 @@ func getRequestForm(w http.ResponseWriter, r *http.Request, h *model.Handler) {
// We tried to exercise this execution path but didn't know how.
err := h.Request.ParseForm()
if err != nil {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
} else if values, ok := h.Request.Form[name]; ok {
_, _ = w.Write([]byte(values[0]))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -133,7 +133,7 @@ func getRequestFileName(w http.ResponseWriter, r *http.Request, h *model.Handler
if err == nil {
_, _ = w.Write([]byte(header.Filename))
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -144,7 +144,7 @@ func getRequestFileContent(w http.ResponseWriter, r *http.Request, h *model.Hand
if err == nil {
_, _ = io.Copy(w, file)
} else {
srverrors.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
httperror.ErrorJSON(w, ResourceItemNotFound, http.StatusNotFound)
}
}
@@ -153,14 +153,14 @@ func getRequestFileContent(w http.ResponseWriter, r *http.Request, h *model.Hand
func setResponseStatus(w http.ResponseWriter, r *http.Request, h *model.Handler) {
sb, err := ioutil.ReadAll(r.Body)
if err != nil {
srverrors.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if si, err := strconv.Atoi(string(sb)); err != nil {
srverrors.ErrorJSON(w, NonIntegerValue, http.StatusUnprocessableEntity)
httperror.ErrorJSON(w, NonIntegerValue, http.StatusUnprocessableEntity)
} else if http.StatusText(si) == "" {
srverrors.ErrorJSON(w, InvalidStatusCode, http.StatusBadRequest)
httperror.ErrorJSON(w, InvalidStatusCode, http.StatusBadRequest)
} else {
h.Writer.WriteHeader(int(si))
}
@@ -170,7 +170,7 @@ func setResponseHeaders(w http.ResponseWriter, r *http.Request, h *model.Handler
name := mux.Vars(r)["name"]
vb, err := ioutil.ReadAll(r.Body)
if err != nil {
srverrors.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -186,7 +186,7 @@ func setResponseCookies(w http.ResponseWriter, r *http.Request, h *model.Handler
name := mux.Vars(r)["name"]
vb, err := ioutil.ReadAll(r.Body)
if err != nil {
srverrors.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
@@ -199,6 +199,6 @@ func setResponseBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
if n > 0 {
panic("Truncated body")
}
srverrors.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
httperror.ErrorJSON(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
+2 -2
View File
@@ -20,7 +20,7 @@ import (
"log"
"net/http"
"github.com/BBVA/kapow/internal/server/srverrors"
"github.com/BBVA/kapow/internal/server/httperror"
"github.com/gorilla/mux"
)
@@ -38,7 +38,7 @@ func configRouter(rs []routeSpec) (r *mux.Router) {
r.HandleFunc(
"/handlers/{handlerID}/{resource:.*}",
func(w http.ResponseWriter, r *http.Request) {
srverrors.ErrorJSON(w, "Invalid Resource Path", http.StatusBadRequest)
httperror.ErrorJSON(w, "Invalid Resource Path", http.StatusBadRequest)
})
return r
}
+2 -2
View File
@@ -24,8 +24,8 @@ import (
"net/http/httptest"
"testing"
"github.com/BBVA/kapow/internal/server/httperror"
"github.com/BBVA/kapow/internal/server/model"
"github.com/BBVA/kapow/internal/server/srverrors"
)
func checkErrorResponse(r *http.Response, expectedErrcode int, expectedReason string) []error {
@@ -39,7 +39,7 @@ func checkErrorResponse(r *http.Response, expectedErrcode int, expectedReason st
errList = append(errList, fmt.Errorf("Content-Type header mismatch. Expected: %q, got: %q", "application/json; charset=utf-8", v))
}
errMsg := srverrors.ServerErrMessage{}
errMsg := httperror.ServerErrMessage{}
if bodyBytes, err := ioutil.ReadAll(r.Body); err != nil {
errList = append(errList, fmt.Errorf("Unexpected error reading response body: %v", err))
} else if err := json.Unmarshal(bodyBytes, &errMsg); err != nil {