This commit is contained in:
withchao
2023-06-11 14:48:30 +08:00
parent a990e6f19d
commit c16e7fc022
3 changed files with 27 additions and 22 deletions
+11 -5
View File
@@ -5,15 +5,21 @@ import (
"net/http"
)
func HttpError(w http.ResponseWriter, err error) {
data, err := json.Marshal(ParseError(err))
func httpJson(w http.ResponseWriter, data any) {
body, err := json.Marshal(data)
if err != nil {
panic(err)
http.Error(w, "json marshal error: "+err.Error(), http.StatusInternalServerError)
return
}
_ = data
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}
func HttpError(w http.ResponseWriter, err error) {
httpJson(w, ParseError(err))
}
func HttpSuccess(w http.ResponseWriter, data any) {
httpJson(w, ApiSuccess(data))
}