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

View File

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