Skip to content

Commit b9fe452

Browse files
author
Derek Dowling
committed
Making send return errors for simpler debugging
1 parent c1405c8 commit b9fe452

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

error_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func TestError(t *testing.T) {
9191
}, testError}}
9292

9393
Convey("should send a properly formatted JSON error list", func() {
94-
Send(writer, request, testErrors)
94+
err := Send(writer, request, testErrors)
95+
So(err, ShouldBeNil)
9596
So(writer.Code, ShouldEqual, http.StatusForbidden)
9697

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

list_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ func TestList(t *testing.T) {
3434
Convey("should send a properly formatted List response", func() {
3535

3636
writer := httptest.NewRecorder()
37-
Send(writer, req, testList)
37+
err := Send(writer, req, testList)
38+
So(err, ShouldBeNil)
3839
So(writer.Code, ShouldEqual, http.StatusOK)
3940

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

object_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ func TestObject(t *testing.T) {
140140
Convey("->Send(Object)", func() {
141141
request.Method = "POST"
142142
writer := httptest.NewRecorder()
143-
Send(writer, request, testObject)
143+
err := Send(writer, request, testObject)
144+
So(err, ShouldBeNil)
144145
So(writer.Code, ShouldEqual, http.StatusCreated)
145146
})
146147
})

parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ const (
4040
// response, err := jsh.NewObject(yourType.ID, "yourtype", &yourType)
4141
// if err != nil {
4242
// // log error
43-
// jsh.Send(w, r, err)
43+
// err := jsh.Send(w, r, err)
4444
// return
4545
// }
4646
//
47-
// jsh.Send(w, r, response)
47+
// err := jsh.Send(w, r, response)
4848
// }
4949
func ParseObject(r *http.Request) (*Object, SendableError) {
5050

response.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package jsh
22

33
import (
44
"encoding/json"
5-
"log"
5+
"fmt"
66
"net/http"
77
"strconv"
88
)
@@ -54,47 +54,54 @@ func (r *Response) Validate() SendableError {
5454
return nil
5555
}
5656

57-
// Send fires a JSON response if the payload is prepared successfully, otherwise it
58-
// returns an Error which can also be sent.
59-
func Send(w http.ResponseWriter, r *http.Request, payload Sendable) {
57+
// Send will return a JSON payload to the requestor. If the payload response validation
58+
// fails, it will send an appropriate error to the requestor and will return the error
59+
func Send(w http.ResponseWriter, r *http.Request, payload Sendable) error {
60+
6061
response, err := payload.prepare(r, true)
6162
if err != nil {
62-
response, err = err.prepare(r, true)
6363

64-
// If we ever hit this, something seriously wrong has happened
64+
response, err = err.prepare(r, true)
6565
if err != nil {
66-
log.Printf("Error preparing JSH error: %s", err.Error())
6766
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
68-
return
67+
return fmt.Errorf("Error preparing JSH error: %s", err.Error())
6968
}
69+
70+
return fmt.Errorf("Error preparing JSON payload: %s", err.Error())
7071
}
7172

72-
SendResponse(w, r, response)
73+
return SendResponse(w, r, response)
7374
}
7475

7576
// SendResponse handles sending a fully packaged JSON Response allows API consumers
7677
// to more manually build their Responses in case they want to send Meta, Links, etc
77-
func SendResponse(w http.ResponseWriter, r *http.Request, response *Response) {
78+
// The function will always, send but will return the last error it encountered
79+
// to help with debugging
80+
func SendResponse(w http.ResponseWriter, r *http.Request, response *Response) error {
7881

7982
err := response.Validate()
8083
if err != nil {
8184
response, err = err.prepare(r, true)
8285

8386
// If we ever hit this, something seriously wrong has happened
8487
if err != nil {
85-
log.Printf("Error preparing JSH error: %s", err.Error())
8688
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
89+
return fmt.Errorf("Error preparing JSH error: %s", err.Error())
8790
}
91+
92+
return fmt.Errorf("Response validation error: %s", err.Error())
8893
}
8994

9095
content, jsonErr := json.MarshalIndent(response, "", " ")
9196
if jsonErr != nil {
92-
log.Printf("Unable to prepare JSON content: %s", jsonErr)
9397
http.Error(w, DefaultErrorTitle, http.StatusInternalServerError)
98+
return fmt.Errorf("Unable to marshal JSON payload: %s", jsonErr.Error())
9499
}
95100

96101
w.Header().Add("Content-Type", ContentType)
97102
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
98103
w.WriteHeader(response.HTTPStatus)
99104
w.Write(content)
105+
106+
return nil
100107
}

response_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func TestSend(t *testing.T) {
3131

3232
request.Method = "GET"
3333

34-
Send(writer, request, object)
34+
err := Send(writer, request, object)
35+
So(err, ShouldBeNil)
3536
So(writer.Code, ShouldEqual, http.StatusOK)
3637

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

0 commit comments

Comments
 (0)