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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user