diff --git a/internal/server/data/resource.go b/internal/server/data/resource.go index 8b1076d..caf3e13 100644 --- a/internal/server/data/resource.go +++ b/internal/server/data/resource.go @@ -24,6 +24,7 @@ import ( "strconv" "github.com/BBVA/kapow/internal/server/model" + "github.com/BBVA/kapow/internal/server/srverrors" "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) if err != nil { if n == 0 { - w.WriteHeader(http.StatusInternalServerError) + srverrors.WriteErrorResponse(http.StatusInternalServerError, "Internal Server Error", w) } else { // Only way to abort current connection as of go 1.13 // https://github.com/golang/go/issues/16542 diff --git a/internal/server/data/resource_test.go b/internal/server/data/resource_test.go index e723f0f..73e7c32 100644 --- a/internal/server/data/resource_test.go +++ b/internal/server/data/resource_test.go @@ -121,9 +121,8 @@ func TestGetRequestBody500sWhenHandlerRequestErrors(t *testing.T) { getRequestBody(w, r, &h) - res := w.Result() - if res.StatusCode != http.StatusInternalServerError { - t.Error("status not 500") + for _, e := range checkErrorResponse(w.Result(), http.StatusInternalServerError, "Internal Server Error") { + t.Error(e.Error()) } } diff --git a/internal/server/srverrors/error.go b/internal/server/srverrors/error.go index cc40f54..83ed512 100644 --- a/internal/server/srverrors/error.go +++ b/internal/server/srverrors/error.go @@ -13,7 +13,7 @@ func WriteErrorResponse(statusCode int, reasonMsg string, res http.ResponseWrite respBody := ServerErrMessage{} respBody.Reason = reasonMsg 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.Write(bb) }