Skip to content

Commit 078353d

Browse files
authored
Bug: Fix test framework issues (#8)
* return byte, error * fix ci error
1 parent 8e8c556 commit 078353d

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

context.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

utils/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package utils
22

33
import "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
}

utils/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func 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
}

0 commit comments

Comments
 (0)