add panic json
parent
6fde850d35
commit
0d61b343a2
7
net.go
7
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
|
||||
|
|
Loading…
Reference in New Issue