Skip to content

Commit

Permalink
Add helpers for common tasks in action handlers
Browse files Browse the repository at this point in the history
This patch also changes internal server error message and action
handlers tests to unify all error messages.
  • Loading branch information
pawiecz committed Jan 2, 2018
1 parent b111c43 commit 2c08026
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
8 changes: 8 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "errors"

var (
// ErrRelayNotFound is returned when Relay is not found.
ErrRelayNotFound = errors.New("relay not found")
)
24 changes: 19 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,36 @@ func RelayIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// RelayShow handles the relays show action (GET /relays/:id).
func RelayShow(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
id := params.ByName("id")
relay, err := findRelay(id)
if err != nil {
writeError(w, http.StatusNotFound, err.Error())
return
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
}

// findRelay locates Relay with the given ID on the module.
func findRelay(id string) (*Relay, error) {
relay, ok := module[id]
if !ok {
// No relay with the ID given in the URL has been found
apiError := &ApiError{Status: http.StatusNotFound, Title: "Relay not found"}
writeResponse(w, http.StatusNotFound, &JsonErrorResponse{Error: apiError})
return
return nil, ErrRelayNotFound
}
writeResponse(w, http.StatusOK, &JsonResponse{Data: relay})
return relay, nil
}

// writeError wraps writing API error response.
func writeError(w http.ResponseWriter, status int, title string) {
apiError := &ApiError{Status: status, Title: title}
writeResponse(w, status, &JsonErrorResponse{Error: apiError})
}

// writeResponse writes standard JSON API response with status code.
func writeResponse(w http.ResponseWriter, status int, response interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(response); err != nil {
apiError := &ApiError{Status: http.StatusInternalServerError, Title: "Internal server error"}
apiError := &ApiError{Status: http.StatusInternalServerError, Title: "internal server error"}
writeResponse(w, http.StatusInternalServerError, apiError)
}
}
2 changes: 1 addition & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Main", func() {
})

It("should return an error on show request for nonexistent relay", func() {
expected := "{\"error\":{\"status\":404,\"title\":\"Relay not found\"}}\n"
expected := "{\"error\":{\"status\":404,\"title\":\"relay not found\"}}\n"
actual := call("GET", "/relays/I0", "/relays/:id", RelayShow)
Expect(actual.Code).To(Equal(http.StatusNotFound))
Expect(actual.Body.String()).To(MatchJSON(expected))
Expand Down

0 comments on commit 2c08026

Please sign in to comment.