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:
@@ -22,7 +22,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
)
|
||||
|
||||
// Reason returns the reason phrase embedded within the JSON error
|
||||
@@ -33,7 +33,7 @@ func Reason(r *http.Response) (string, error) {
|
||||
return "", errors.New("error reading response's body")
|
||||
}
|
||||
|
||||
reason := &srverrors.ServerErrMessage{}
|
||||
reason := &httperror.ServerErrMessage{}
|
||||
err = json.Unmarshal(body, reason)
|
||||
if err != nil {
|
||||
return "", errors.New("error unmarshaling JSON")
|
||||
|
||||
@@ -24,8 +24,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/BBVA/kapow/internal/server/user"
|
||||
)
|
||||
|
||||
@@ -53,7 +53,7 @@ func removeRoute(res http.ResponseWriter, req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
id := vars["id"]
|
||||
if err := funcRemove(id); err != nil {
|
||||
srverrors.ErrorJSON(res, "Route Not Found", http.StatusNotFound)
|
||||
httperror.ErrorJSON(res, "Route Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -94,29 +94,29 @@ func addRoute(res http.ResponseWriter, req *http.Request) {
|
||||
payload, _ := ioutil.ReadAll(req.Body)
|
||||
err := json.Unmarshal(payload, &route)
|
||||
if err != nil {
|
||||
srverrors.ErrorJSON(res, "Malformed JSON", http.StatusBadRequest)
|
||||
httperror.ErrorJSON(res, "Malformed JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if route.Method == "" {
|
||||
srverrors.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
httperror.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
if route.Pattern == "" {
|
||||
srverrors.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
httperror.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
err = pathValidator(route.Pattern)
|
||||
if err != nil {
|
||||
srverrors.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
httperror.ErrorJSON(res, "Invalid Route", http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := idGenerator()
|
||||
if err != nil {
|
||||
srverrors.ErrorJSON(res, "Internal Server Error", http.StatusInternalServerError)
|
||||
httperror.ErrorJSON(res, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ var funcGet func(string) (model.Route, error) = user.Routes.Get
|
||||
func getRoute(res http.ResponseWriter, req *http.Request) {
|
||||
id := mux.Vars(req)["id"]
|
||||
if r, err := funcGet(id); err != nil {
|
||||
srverrors.ErrorJSON(res, "Route Not Found", http.StatusNotFound)
|
||||
httperror.ErrorJSON(res, "Route Not Found", http.StatusNotFound)
|
||||
} else {
|
||||
res.Header().Set("Content-Type", "application/json")
|
||||
rBytes, _ := json.Marshal(r)
|
||||
|
||||
@@ -30,8 +30,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
"github.com/BBVA/kapow/internal/server/model"
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/BBVA/kapow/internal/server/user"
|
||||
)
|
||||
|
||||
@@ -46,7 +46,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 {
|
||||
@@ -118,10 +118,10 @@ func TestPathValidatorErrorWhenInvalidPath(t *testing.T) {
|
||||
|
||||
func TestAddRouteReturnsBadRequestWhenMalformedJSONBody(t *testing.T) {
|
||||
reqPayload := `{
|
||||
method": "GET",
|
||||
url_pattern": "/hello",
|
||||
entrypoint": null,
|
||||
command": "echo Hello World | kapow set /response/body"
|
||||
method": "GET",
|
||||
url_pattern": "/hello",
|
||||
entrypoint": null,
|
||||
command": "echo Hello World | kapow set /response/body"
|
||||
}`
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/routes", strings.NewReader(reqPayload))
|
||||
@@ -141,46 +141,46 @@ func TestAddRouteReturns422ErrorWhenMandatoryFieldsMissing(t *testing.T) {
|
||||
}{
|
||||
{`{}`, "EmptyBody", true},
|
||||
{`{
|
||||
"method": "GET"
|
||||
}`,
|
||||
"method": "GET"
|
||||
}`,
|
||||
"Missing url_pattern",
|
||||
true,
|
||||
},
|
||||
{`{
|
||||
"url_pattern": "/hello"
|
||||
}`,
|
||||
"url_pattern": "/hello"
|
||||
}`,
|
||||
"Missing method",
|
||||
true,
|
||||
},
|
||||
{`{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello"
|
||||
}`,
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello"
|
||||
}`,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{`{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": ""
|
||||
}`,
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": ""
|
||||
}`,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{`{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"command": ""
|
||||
}`,
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"command": ""
|
||||
}`,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{`{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "",
|
||||
"command": ""
|
||||
}`,
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "",
|
||||
"command": ""
|
||||
}`,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
@@ -210,10 +210,10 @@ func TestAddRouteReturns422ErrorWhenMandatoryFieldsMissing(t *testing.T) {
|
||||
|
||||
func TestAddRouteGeneratesRouteID(t *testing.T) {
|
||||
reqPayload := `{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/routes", strings.NewReader(reqPayload))
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -236,10 +236,10 @@ func TestAddRouteGeneratesRouteID(t *testing.T) {
|
||||
|
||||
func TestAddRoute500sWhenIDGeneratorFails(t *testing.T) {
|
||||
reqPayload := `{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/routes", strings.NewReader(reqPayload))
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -264,10 +264,10 @@ func TestAddRoute500sWhenIDGeneratorFails(t *testing.T) {
|
||||
|
||||
func TestAddRouteReturnsCreated(t *testing.T) {
|
||||
reqPayload := `{
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
"method": "GET",
|
||||
"url_pattern": "/hello",
|
||||
"entrypoint": "/bin/sh -c",
|
||||
"command": "echo Hello World | kapow set /response/body"
|
||||
}`
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/routes", strings.NewReader(reqPayload))
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package srverrors
|
||||
package httperror
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -1,4 +1,4 @@
|
||||
package srverrors_test
|
||||
package httperror_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
)
|
||||
|
||||
func TestErrorJSONSetsAppJsonContentType(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srverrors.ErrorJSON(w, "Not Important Here", 0)
|
||||
httperror.ErrorJSON(w, "Not Important Here", 0)
|
||||
|
||||
if v := w.Result().Header.Get("Content-Type"); v != "application/json; charset=utf-8" {
|
||||
t.Errorf("Content-Type header mismatch. Expected: %q, got: %q", "application/json; charset=utf-8", v)
|
||||
@@ -23,7 +23,7 @@ func TestErrorJSONSetsAppJsonContentType(t *testing.T) {
|
||||
func TestErrorJSONSetsRequestedStatusCode(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srverrors.ErrorJSON(w, "Not Important Here", http.StatusGone)
|
||||
httperror.ErrorJSON(w, "Not Important Here", http.StatusGone)
|
||||
|
||||
if v := w.Result().StatusCode; v != http.StatusGone {
|
||||
t.Errorf("Status code mismatch. Expected: %d, got: %d", http.StatusGone, v)
|
||||
@@ -34,9 +34,9 @@ func TestErrorJSONSetsBodyCorrectly(t *testing.T) {
|
||||
expectedReason := "Something Not Found"
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
srverrors.ErrorJSON(w, expectedReason, http.StatusNotFound)
|
||||
httperror.ErrorJSON(w, expectedReason, http.StatusNotFound)
|
||||
|
||||
errMsg := srverrors.ServerErrMessage{}
|
||||
errMsg := httperror.ServerErrMessage{}
|
||||
if bodyBytes, err := ioutil.ReadAll(w.Result().Body); err != nil {
|
||||
t.Errorf("Unexpected error reading response body: %v", err)
|
||||
} else if err := json.Unmarshal(bodyBytes, &errMsg); err != nil {
|
||||
Reference in New Issue
Block a user