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) {
208
208
}
209
209
210
210
// 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 ) {
212
212
c .RequestCtx .Response .Header .SetContentType ("application/json" )
213
213
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
215
221
}
Original file line number Diff line number Diff line change @@ -2,10 +2,10 @@ package utils
2
2
3
3
import "encoding/json"
4
4
5
- func ToJSON (v interface {}) string {
5
+ func ToJSON (v interface {}) ( string , error ) {
6
6
b , err := json .Marshal (v )
7
7
if err != nil {
8
- return ""
8
+ return "" , err
9
9
}
10
- return string (b )
10
+ return string (b ), nil
11
11
}
Original file line number Diff line number Diff line change 7
7
8
8
func TestToJSON (t * testing.T ) {
9
9
t .Parallel ()
10
- res := ToJSON ("MY/NAME/IS/:PARAM/*" )
10
+ res , _ := ToJSON ("MY/NAME/IS/:PARAM/*" )
11
11
12
12
fmt .Println (res )
13
13
}
You can’t perform that action at this time.
0 commit comments