fix:update response err

This commit is contained in:
kenneth 2023-12-25 18:01:39 +08:00
parent b7c593c827
commit de0cf3f4e6
2 changed files with 22 additions and 2 deletions

View File

@ -33,3 +33,23 @@ func respond(w http.ResponseWriter, message string, v any, statusCode int) {
log.Printf("could not write http response: %v\n", err)
}
}
func RespondErr(w http.ResponseWriter, message string, v any) {
rsp := response{
Success: false,
Message: message,
Data: v,
}
b, err := json.Marshal(rsp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write(b)
if err != nil && !errors.Is(err, context.Canceled) {
log.Printf("could not write http response: %v\n", err)
}
}

View File

@ -68,12 +68,12 @@ func DeleteShortUrl(store db.Store) http.HandlerFunc {
Status: -1,
})
if err != nil {
respond(w, "删除错误", nil, http.StatusOK)
RespondErr(w, "删除错误", nil)
return
}
err = service.DeleteShortUrl(shorUrl)
if err != nil {
respond(w, "删除错误", nil, http.StatusOK)
RespondErr(w, "删除错误", nil)
return
}