From a5f7701437ec016a0de99b1f63988838bf6a64f5 Mon Sep 17 00:00:00 2001 From: Alexander Cobleigh Date: Tue, 20 Apr 2021 09:56:30 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Henry <111202+cryptix@users.noreply.github.com> use eh.Handle --- web/handlers/admin/settings.go | 8 ++++---- web/handlers/http.go | 6 ++++-- web/handlers/set_language_test.go | 1 - 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/web/handlers/admin/settings.go b/web/handlers/admin/settings.go index 5d29fa30..e18c2eca 100644 --- a/web/handlers/admin/settings.go +++ b/web/handlers/admin/settings.go @@ -115,13 +115,13 @@ func (h settingsHandler) getMember(w http.ResponseWriter, req *http.Request) *ro func (h settingsHandler) verifyPostRequirements(w http.ResponseWriter, req *http.Request) bool { if req.Method != "POST" { - // TODO: proper error type - h.r.Error(w, req, http.StatusBadRequest, fmt.Errorf("bad request")) + err := weberrors.ErrBadRequest{Where: "HTTP Method", Details: fmt.Errorf("expected POST not %s", req.Method)} + h.r.Error(w, req, http.StatusBadRequest, err) return false } if err := req.ParseForm(); err != nil { - // TODO: proper error type - h.r.Error(w, req, http.StatusBadRequest, fmt.Errorf("bad request: %w", err)) + err = weberrors.ErrBadRequest{Where: "Form data", Details: err} + h.r.Error(w, req, http.StatusBadRequest, err) return false } return true diff --git a/web/handlers/http.go b/web/handlers/http.go index 76e326fe..e9697161 100644 --- a/web/handlers/http.go +++ b/web/handlers/http.go @@ -283,14 +283,16 @@ func New( session, err := cookieStore.Get(req, i18n.LanguageCookieName) if err != nil { - fmt.Errorf("cookie error? %w\n", err) + eh.Handle(w, req, http.StatusInternalServerError,err) return } session.Values["lang"] = lang err = session.Save(req, w) if err != nil { - fmt.Errorf("we failed to save the language session cookie %w\n", err) + err = fmt.Errorf("we failed to save the language session cookie %w\n", err) + eh.Handle(w, req, http.StatusInternalServerError, err) + return } http.Redirect(w, req, previousRoute, http.StatusSeeOther) diff --git a/web/handlers/set_language_test.go b/web/handlers/set_language_test.go index ea20502e..97ba34eb 100644 --- a/web/handlers/set_language_test.go +++ b/web/handlers/set_language_test.go @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/assert" - // "github.com/ssb-ngi-pointer/go-ssb-room/roomdb" "github.com/ssb-ngi-pointer/go-ssb-room/web/i18n" "github.com/ssb-ngi-pointer/go-ssb-room/web/router" )