New error handling added to getRequestBody function in data package
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/BBVA/kapow/internal/server/model"
|
"github.com/BBVA/kapow/internal/server/model"
|
||||||
|
"github.com/BBVA/kapow/internal/server/srverrors"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ func getRequestBody(w http.ResponseWriter, r *http.Request, h *model.Handler) {
|
|||||||
n, err := io.Copy(w, h.Request.Body)
|
n, err := io.Copy(w, h.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
srverrors.WriteErrorResponse(http.StatusInternalServerError, "Internal Server Error", w)
|
||||||
} else {
|
} else {
|
||||||
// Only way to abort current connection as of go 1.13
|
// Only way to abort current connection as of go 1.13
|
||||||
// https://github.com/golang/go/issues/16542
|
// https://github.com/golang/go/issues/16542
|
||||||
|
|||||||
@@ -121,9 +121,8 @@ func TestGetRequestBody500sWhenHandlerRequestErrors(t *testing.T) {
|
|||||||
|
|
||||||
getRequestBody(w, r, &h)
|
getRequestBody(w, r, &h)
|
||||||
|
|
||||||
res := w.Result()
|
for _, e := range checkErrorResponse(w.Result(), http.StatusInternalServerError, "Internal Server Error") {
|
||||||
if res.StatusCode != http.StatusInternalServerError {
|
t.Error(e.Error())
|
||||||
t.Error("status not 500")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func WriteErrorResponse(statusCode int, reasonMsg string, res http.ResponseWrite
|
|||||||
respBody := ServerErrMessage{}
|
respBody := ServerErrMessage{}
|
||||||
respBody.Reason = reasonMsg
|
respBody.Reason = reasonMsg
|
||||||
bb, _ := json.Marshal(respBody)
|
bb, _ := json.Marshal(respBody)
|
||||||
res.Header().Add("Content-Type", "application/json; charset=utf-8")
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
res.WriteHeader(statusCode)
|
res.WriteHeader(statusCode)
|
||||||
_, _ = res.Write(bb)
|
_, _ = res.Write(bb)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user