File tree Expand file tree Collapse file tree 3 files changed +12
-6
lines changed Expand file tree Collapse file tree 3 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -208,8 +208,14 @@ func (c *Context) Status(code int) {
208208}
209209
210210// JSON sets the response body to the given JSON representation.
211- func (c * Context ) JSON (code int , obj interface {}) {
211+ func (c * Context ) JSON (code int , obj interface {}) ([] byte , error ) {
212212 c .RequestCtx .Response .Header .SetContentType ("application/json" )
213213 c .RequestCtx .Response .SetStatusCode (code )
214- c .RequestCtx .Response .SetBodyString (utils .ToJSON (obj ))
214+ jsonBody , err := utils .ToJSON (obj )
215+ if err != nil {
216+ return nil , err
217+ }
218+ c .RequestCtx .Response .SetBodyString (jsonBody )
219+
220+ return []byte (jsonBody ), nil
215221}
Original file line number Diff line number Diff line change @@ -2,10 +2,10 @@ package utils
22
33import "encoding/json"
44
5- func ToJSON (v interface {}) string {
5+ func ToJSON (v interface {}) ( string , error ) {
66 b , err := json .Marshal (v )
77 if err != nil {
8- return ""
8+ return "" , err
99 }
10- return string (b )
10+ return string (b ), nil
1111}
Original file line number Diff line number Diff line change 77
88func TestToJSON (t * testing.T ) {
99 t .Parallel ()
10- res := ToJSON ("MY/NAME/IS/:PARAM/*" )
10+ res , _ := ToJSON ("MY/NAME/IS/:PARAM/*" )
1111
1212 fmt .Println (res )
1313}
You can’t perform that action at this time.
0 commit comments