@@ -2,7 +2,7 @@ package jsh
2
2
3
3
import (
4
4
"encoding/json"
5
- "log "
5
+ "fmt "
6
6
"net/http"
7
7
"strconv"
8
8
)
@@ -54,47 +54,54 @@ func (r *Response) Validate() SendableError {
54
54
return nil
55
55
}
56
56
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
+
60
61
response , err := payload .prepare (r , true )
61
62
if err != nil {
62
- response , err = err .prepare (r , true )
63
63
64
- // If we ever hit this, something seriously wrong has happened
64
+ response , err = err . prepare ( r , true )
65
65
if err != nil {
66
- log .Printf ("Error preparing JSH error: %s" , err .Error ())
67
66
http .Error (w , DefaultErrorTitle , http .StatusInternalServerError )
68
- return
67
+ return fmt . Errorf ( "Error preparing JSH error: %s" , err . Error ())
69
68
}
69
+
70
+ return fmt .Errorf ("Error preparing JSON payload: %s" , err .Error ())
70
71
}
71
72
72
- SendResponse (w , r , response )
73
+ return SendResponse (w , r , response )
73
74
}
74
75
75
76
// SendResponse handles sending a fully packaged JSON Response allows API consumers
76
77
// 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 {
78
81
79
82
err := response .Validate ()
80
83
if err != nil {
81
84
response , err = err .prepare (r , true )
82
85
83
86
// If we ever hit this, something seriously wrong has happened
84
87
if err != nil {
85
- log .Printf ("Error preparing JSH error: %s" , err .Error ())
86
88
http .Error (w , DefaultErrorTitle , http .StatusInternalServerError )
89
+ return fmt .Errorf ("Error preparing JSH error: %s" , err .Error ())
87
90
}
91
+
92
+ return fmt .Errorf ("Response validation error: %s" , err .Error ())
88
93
}
89
94
90
95
content , jsonErr := json .MarshalIndent (response , "" , " " )
91
96
if jsonErr != nil {
92
- log .Printf ("Unable to prepare JSON content: %s" , jsonErr )
93
97
http .Error (w , DefaultErrorTitle , http .StatusInternalServerError )
98
+ return fmt .Errorf ("Unable to marshal JSON payload: %s" , jsonErr .Error ())
94
99
}
95
100
96
101
w .Header ().Add ("Content-Type" , ContentType )
97
102
w .Header ().Set ("Content-Length" , strconv .Itoa (len (content )))
98
103
w .WriteHeader (response .HTTPStatus )
99
104
w .Write (content )
105
+
106
+ return nil
100
107
}
0 commit comments