diff options
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r-- | internal/handlers/main.go | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go index e87c74f..e470b49 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -47,7 +47,8 @@ func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseGlob("components/*.html") if err != nil { - panic(err) + abortWithError(w, err.Error()) + return } usernameRaw := r.Context().Value("username") h.log.Info("got mainpage request", "username", usernameRaw) @@ -68,7 +69,8 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { } userScore.Actions, err = h.repo.DBActionList(username) if err != nil { - panic(err) + abortWithError(w, err.Error()) + return } tmpl.ExecuteTemplate(w, "main", userScore) } @@ -78,13 +80,12 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { h.log.Info("got postform request", "payload", r.PostForm) magnitude := uint8(1) mS := r.PostFormValue("magnitude") - h.log.Info("showing magnitude send", "mS", mS) if mS != "1" { u64, err := strconv.ParseUint(mS, 10, 64) magnitude = uint8(u64) if err != nil { - // TODO: error handling - h.log.Warn("got an error", "error", err) + h.log.Warn("failed to parse magnitude", "error", err, + "payload", r.PostForm) magnitude = uint8(1) } } @@ -115,11 +116,13 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { h.log.Info("got username from ctx", "username", username) userScore, err := h.repo.DBUserScoreGet(username) if err != nil { - panic(err) + abortWithError(w, err.Error()) + return } act.Username = userScore.Username if err := h.repo.DBActionCreate(&act); err != nil { - panic(err) + abortWithError(w, err.Error()) + return } http.Redirect(w, r, "/", 302) } @@ -147,7 +150,8 @@ func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { "username", username) userScore, err := h.UserScoreWithActionsByUsername(username) if err != nil { - panic(err) + abortWithError(w, err.Error()) + return } // get action by name action, err := h.repo.DBActionGetByName(actionName) @@ -159,11 +163,13 @@ func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { userScore.Score += magnitude // disable action if repetable if err := h.repo.DBActionDone(actionName); err != nil { - panic(err) + abortWithError(w, err.Error()) + return } // update score in db if err := h.repo.DBUserScoreUpdate(userScore); err != nil { - panic(err) + abortWithError(w, err.Error()) + return } tmpl := template.Must(template.ParseGlob("components/*.html")) tmpl.ExecuteTemplate(w, "main", userScore) |