Skip to content

Commit

Permalink
fix: write endpoint errors out gracefully (#18743)
Browse files Browse the repository at this point in the history
  • Loading branch information
JadhavPoonam authored Sep 12, 2023
1 parent 697836b commit 264166f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/resource/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ func (h *resourceHandler) handleWrite(w http.ResponseWriter, r *http.Request, ct
var req writeRequest
// convert req body to writeRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.logger.Error("Failed to decode request body", "error", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Request body didn't follow schema."))
w.Write([]byte("Request body format is invalid"))
return
}
// convert data struct to proto message
data := h.reg.Proto.ProtoReflect().New().Interface()
if err := protojson.Unmarshal(req.Data, data); err != nil {
h.logger.Error("Failed to unmarshal to proto message", "error", err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Request body didn't follow schema."))
w.Write([]byte("Request body didn't follow the resource schema"))
return
}
// proto message to any
anyProtoMsg, err := anypb.New(data)
Expand Down
20 changes: 20 additions & 0 deletions internal/resource/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package http
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -41,6 +42,7 @@ func TestResourceHandler_InputValidation(t *testing.T) {
request *http.Request
response *httptest.ResponseRecorder
expectedResponseCode int
expectedErrorMessage string
}
client := svctest.RunResourceService(t, demo.RegisterTypes)
resourceHandler := resourceHandler{
Expand Down Expand Up @@ -70,6 +72,7 @@ func TestResourceHandler_InputValidation(t *testing.T) {
`)),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
expectedErrorMessage: "rpc error: code = InvalidArgument desc = resource.id.name is required",
},
{
description: "wrong schema",
Expand All @@ -86,20 +89,37 @@ func TestResourceHandler_InputValidation(t *testing.T) {
`)),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
expectedErrorMessage: "Request body didn't follow the resource schema",
},
{
description: "invalid request body",
request: httptest.NewRequest("PUT", "/keith-urban?partition=default&peer_name=local&namespace=default", strings.NewReader("bad-input")),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
expectedErrorMessage: "Request body format is invalid",
},
{
description: "no id",
request: httptest.NewRequest("DELETE", "/?partition=default&peer_name=local&namespace=default", strings.NewReader("")),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
expectedErrorMessage: "rpc error: code = InvalidArgument desc = id.name is required",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
resourceHandler.ServeHTTP(tc.response, tc.request)

response := tc.response.Result()

defer response.Body.Close()

b, err := io.ReadAll(tc.response.Body)
require.NoError(t, err)

require.Equal(t, tc.expectedResponseCode, tc.response.Result().StatusCode)
require.Equal(t, tc.expectedErrorMessage, string(b))
})
}
}
Expand Down

0 comments on commit 264166f

Please sign in to comment.