Skip to content

Commit

Permalink
Removing error responses from Send functions, adding default 500 ISE …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
Derek Dowling committed Nov 17, 2015
1 parent a471868 commit 2d1b24b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
7 changes: 2 additions & 5 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func TestError(t *testing.T) {
})

Convey("->Send()", func() {
err := Send(writer, request, testError)
So(err, ShouldBeNil)
Send(writer, request, testError)
So(writer.Code, ShouldEqual, http.StatusBadRequest)
})

Expand Down Expand Up @@ -92,9 +91,7 @@ func TestError(t *testing.T) {
}, testError}}

Convey("should send a properly formatted JSON error list", func() {
err := Send(writer, request, testErrors)

So(err, ShouldBeNil)
Send(writer, request, testErrors)
So(writer.Code, ShouldEqual, http.StatusForbidden)

contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
Expand Down
3 changes: 1 addition & 2 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func TestList(t *testing.T) {
Convey("should send a properly formatted List response", func() {

writer := httptest.NewRecorder()
err := Send(writer, req, testList)
So(err, ShouldBeNil)
Send(writer, req, testList)
So(writer.Code, ShouldEqual, http.StatusOK)

contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
Expand Down
3 changes: 1 addition & 2 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ func TestObject(t *testing.T) {
Convey("->Send(Object)", func() {
request.Method = "POST"
writer := httptest.NewRecorder()
err := Send(writer, request, testObject)
So(err, ShouldBeNil)
Send(writer, request, testObject)
So(writer.Code, ShouldEqual, http.StatusCreated)
})
})
Expand Down
33 changes: 22 additions & 11 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jsh

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
)
Expand Down Expand Up @@ -33,7 +33,7 @@ type Response struct {
}

// Validate checks JSON Spec for the top level JSON document
func (r *Response) Validate() *Error {
func (r *Response) Validate() SendableError {

if r.Errors == nil && r.Data == nil {
return ISE("Both `errors` and `data` cannot be blank for a JSON response")
Expand All @@ -56,34 +56,45 @@ func (r *Response) Validate() *Error {

// Send fires a JSON response if the payload is prepared successfully, otherwise it
// returns an Error which can also be sent.
func Send(w http.ResponseWriter, r *http.Request, payload Sendable) SendableError {
func Send(w http.ResponseWriter, r *http.Request, payload Sendable) {
response, err := payload.Prepare(r)
if err != nil {
return err
response, err = err.Prepare(r)

// If we ever hit this, something seriously wrong has happened
if err != nil {
log.Printf("Error preparing JSH error: %s", err.Error())
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
return
}
}

return SendResponse(w, r, response)
SendResponse(w, r, response)
}

// SendResponse handles sending a fully packaged JSON Response allows API consumers
// to more manually build their Responses in case they want to send Meta, Links, etc
func SendResponse(w http.ResponseWriter, r *http.Request, response *Response) SendableError {
func SendResponse(w http.ResponseWriter, r *http.Request, response *Response) {

err := response.Validate()
if err != nil {
return err
response, err = err.Prepare(r)

// If we ever hit this, something seriously wrong has happened
if err != nil {
log.Printf("Error preparing JSH error: %s", err.Error())
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
}
}

content, jsonErr := json.MarshalIndent(response, "", " ")
if jsonErr != nil {
// Sendception
return ISE(fmt.Sprintf("Unable to prepare payload JSON: %s", jsonErr))
log.Printf("Unable to prepare payload JSON: %s", jsonErr)
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
}

w.Header().Add("Content-Type", ContentType)
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
w.WriteHeader(response.HTTPStatus)
w.Write(content)

return nil
}
3 changes: 1 addition & 2 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func TestSend(t *testing.T) {

request.Method = "GET"

err := Send(writer, request, object)
So(err, ShouldBeNil)
Send(writer, request, object)
So(writer.Code, ShouldEqual, http.StatusOK)

contentLength, convErr := strconv.Atoi(writer.HeaderMap.Get("Content-Length"))
Expand Down

0 comments on commit 2d1b24b

Please sign in to comment.