fix:update response err
This commit is contained in:
parent
b7c593c827
commit
de0cf3f4e6
@ -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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -68,12 +68,12 @@ func DeleteShortUrl(store db.Store) http.HandlerFunc {
|
|||||||
Status: -1,
|
Status: -1,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respond(w, "删除错误", nil, http.StatusOK)
|
RespondErr(w, "删除错误", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = service.DeleteShortUrl(shorUrl)
|
err = service.DeleteShortUrl(shorUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respond(w, "删除错误", nil, http.StatusOK)
|
RespondErr(w, "删除错误", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user