Skip to content

Commit

Permalink
chore: errors are sent as json (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
notnmeyer authored Oct 22, 2023
1 parent b09eb7f commit 425bff7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ func handler(w http.ResponseWriter, r *http.Request) {
responseCode, err := validateResponseCode(r.Header)
if err != nil {
responseBody = err.Error()
contentType = "text/plain; charset=utf-8"
} else if !isJSON(responseBody) {
responseBody = fmt.Errorf("x-response-json must be valid JSON").Error()
responseBody = errorResponseFormatter("x-response-json must be valid JSON").Error()
responseCode = http.StatusBadRequest
contentType = "text/plain; charset=utf-8"
}

w.Header().Set("Content-Type", contentType)
Expand All @@ -58,12 +56,12 @@ func validateResponseCode(header map[string][]string) (int, error) {
// verify its a number
code, err := strconv.Atoi(val[0])
if err != nil {
return http.StatusBadRequest, fmt.Errorf("x-response-code must be a number\n")
return http.StatusBadRequest, errorResponseFormatter("x-response-code must be a number")
}

// verify it falls in the range of status codes
if !(code >= 100 && code <= 599) {
return http.StatusBadRequest, fmt.Errorf("x-response-code must be between 100 and 599\n")
return http.StatusBadRequest, errorResponseFormatter("x-response-code must be between 100 and 599")
}

return code, nil
Expand All @@ -77,3 +75,7 @@ func isJSON(s string) bool {
var j json.RawMessage
return json.Unmarshal([]byte(s), &j) == nil
}

func errorResponseFormatter(msg string) error {
return fmt.Errorf(`{"error":"%s"}`, msg)
}
25 changes: 11 additions & 14 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ func TestHandlerValidRequest(t *testing.T) {
}

func TestHandlerWithInvalidXResponseJson(t *testing.T) {
expectedContentType := "text/plain; charset=utf-8"
invalidResponseBody := `{"foo":bar}`
expectedResponseBody := "x-response-json must be valid JSON"
expectedResponseBody := `{"error":"x-response-json must be valid JSON"}`
expectedResponseCode := http.StatusBadRequest

// set up the request
Expand All @@ -69,16 +68,10 @@ func TestHandlerWithInvalidXResponseJson(t *testing.T) {
if rr.Body.String() != expectedResponseBody {
t.Errorf("handler returned unexpected body: got '%s' want '%s'", rr.Body.String(), expectedResponseBody)
}

// content-type
if rr.Header()["Content-Type"][0] != expectedContentType {
t.Errorf("handler returned wrong content-type: got '%s' wanted '%s'", rr.Header()["Content-Type"][0], expectedContentType)
}
}

func TestHandlerWithInvalidXResponseCode(t *testing.T) {
expectedContentType := "text/plain; charset=utf-8"
expectedResponseBody := "x-response-code must be a number\n"
expectedResponseBody := `{"error":"x-response-code must be a number"}`
invalidResponseCode := "blah"
expectedResponseCode := http.StatusBadRequest

Expand All @@ -104,11 +97,6 @@ func TestHandlerWithInvalidXResponseCode(t *testing.T) {
if rr.Body.String() != expectedResponseBody {
t.Errorf("handler returned unexpected body: got '%s' want '%s'", rr.Body.String(), expectedResponseBody)
}

// content-type
if rr.Header()["Content-Type"][0] != expectedContentType {
t.Errorf("handler returned wrong content-type: got '%s' wanted '%s'", rr.Header()["Content-Type"][0], expectedContentType)
}
}

func TestValidateResponseCode(t *testing.T) {
Expand Down Expand Up @@ -145,3 +133,12 @@ func TestIsJSON(t *testing.T) {
t.Errorf("expected %s to be invalid JSON\n", invalid)
}
}

func TestErrorResponseFormatter(t *testing.T) {
expectedErr := `{"error":"hello"}`
err := errorResponseFormatter("hello")

if err.Error() != expectedErr {
t.Errorf("got %s want %s\n", err.Error(), expectedErr)
}
}

0 comments on commit 425bff7

Please sign in to comment.