add panic json

master
Evan Chen 2021-11-12 14:09:11 +08:00
parent 6fde850d35
commit 0d61b343a2
1 changed files with 4 additions and 3 deletions

7
net.go
View File

@ -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