You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
896 B
30 lines
896 B
package jsend
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func Error(w http.ResponseWriter, message string) {
|
|
w.WriteHeader(500)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte("{\"status\": \"error\", \"message\": " + string(message) + "}"))
|
|
}
|
|
|
|
func ErrorWithCode(w http.ResponseWriter, code int, message string) {
|
|
w.WriteHeader(500)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte("{\"status\": \"error\", \"message\": " + string(message) + ", \"code\":" + strconv.Itoa(code) + "}"))
|
|
}
|
|
|
|
func ErrorWithData(w http.ResponseWriter, message string, data map[string]interface{}) {
|
|
jdata, err := json.Marshal(data)
|
|
if err != nil {
|
|
jdata = []byte("null")
|
|
}
|
|
w.WriteHeader(500)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte("{\"status\": \"error\", \"message\": " + string(message) + ", \"data\":" + string(jdata) + "}"))
|
|
}
|