http.Error() func is used to send error messages and codes.

This commit is contained in:
Alexander Andreev 2022-10-20 23:16:30 +04:00
parent 93e1344846
commit be2f34f629
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
1 changed files with 9 additions and 14 deletions

View File

@ -38,14 +38,12 @@ func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
captchaImage, err := inmemdb.Image(captchaID, captchaStyle)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, errMsgImageNotFound)
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
return
}
if captchaImage == nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, errMsgFailedToGenImage)
http.Error(w, errMsgFailedToGenImage, http.StatusInternalServerError)
return
}
@ -62,14 +60,12 @@ func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
ok, err := inmemdb.Solve(captchaID, answer)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, errMsgNotFound)
http.Error(w, errMsgNotFound, http.StatusNotFound)
return
}
if !ok {
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, errMsgWrongAnswer)
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
return
}
@ -80,22 +76,21 @@ func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
captchaID := captcha.ID(server.GetURLParam(r, "captcha"))
isJustRemove := r.URL.Query().Has("remove")
w.WriteHeader(http.StatusNoContent)
if isJustRemove {
inmemdb.Remove(captchaID)
w.WriteHeader(http.StatusNoContent)
return
}
solved, err := inmemdb.IsSolved(captchaID)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, errMsgNotFound)
http.Error(w, errMsgNotFound, http.StatusNotFound)
return
}
if !solved {
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, errMsgWrongAnswer)
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
}
w.WriteHeader(http.StatusNoContent)
}