diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index e586dc1..d00327a 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -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) }