From 0d61b343a29809519e5c92f3e3d7052f0d46ac4d Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Fri, 12 Nov 2021 14:09:11 +0800 Subject: [PATCH] add panic json --- net.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/net.go b/net.go index 7c62d29..af7c687 100644 --- a/net.go +++ b/net.go @@ -36,13 +36,13 @@ func Response(w http.ResponseWriter, status int, body []byte) (int, error) { return w.Write(body) } -func JSON(w http.ResponseWriter, value interface{}) { +func JSON(w http.ResponseWriter, value interface{}) (int, error) { data, err := json.Marshal(value) if err != nil { panic(err) } w.Header().Set("Content-Type", "application/json") - w.Write(data) + return w.Write(data) } // Abort shorthand for aborting with error, strings and status code could also be passed @@ -53,7 +53,8 @@ func Abort(w http.ResponseWriter, errs ...interface{}) (int, error) { switch v := err.(type) { case Error: code = v.Code - msg = v.Json() + w.WriteHeader(code) + return JSON(w, v) case int: if v >= 100 || v < 600 { code = v