From 2d1b24b7019e7b1009e9206dfc7d74fb9e8f3512 Mon Sep 17 00:00:00 2001 From: Derek Dowling Date: Mon, 16 Nov 2015 23:52:47 -0800 Subject: [PATCH] Removing error responses from Send functions, adding default 500 ISE support --- error_test.go | 7 ++----- list_test.go | 3 +-- object_test.go | 3 +-- response.go | 33 ++++++++++++++++++++++----------- response_test.go | 3 +-- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/error_test.go b/error_test.go index c66aaf9..e76e2b0 100644 --- a/error_test.go +++ b/error_test.go @@ -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) }) @@ -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")) diff --git a/list_test.go b/list_test.go index 5bde954..ff5c588 100644 --- a/list_test.go +++ b/list_test.go @@ -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")) diff --git a/object_test.go b/object_test.go index be37cf2..8be6aaf 100644 --- a/object_test.go +++ b/object_test.go @@ -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) }) }) diff --git a/response.go b/response.go index e52dbe3..9cba63c 100644 --- a/response.go +++ b/response.go @@ -2,7 +2,7 @@ package jsh import ( "encoding/json" - "fmt" + "log" "net/http" "strconv" ) @@ -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") @@ -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 } diff --git a/response_test.go b/response_test.go index 4c2e2b6..a3563e5 100644 --- a/response_test.go +++ b/response_test.go @@ -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"))