Skip to content

Commit

Permalink
Improve error page
Browse files Browse the repository at this point in the history
  • Loading branch information
taiidani committed Sep 30, 2023
1 parent f8240cc commit ed17407
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 75 deletions.
16 changes: 16 additions & 0 deletions app/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package app

import "github.com/gin-gonic/gin"

type errorBag struct {
baseBag
Message error
}

func errorResponse(c *gin.Context, code int, err error) {
data := errorBag{
Message: err,
}

c.HTML(code, "error.gohtml", data)
}
11 changes: 6 additions & 5 deletions app/guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"errors"
"fmt"
"guess_my_word/internal/datastore"
"guess_my_word/internal/model"
Expand Down Expand Up @@ -34,29 +35,29 @@ func GuessHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

word := strings.TrimSpace(c.Request.PostFormValue("word"))

// Validate the guess
if len(word) == 0 {
c.HTML(http.StatusBadRequest, "error.gohtml", ErrEmptyGuess)
errorResponse(c, http.StatusBadRequest, errors.New(ErrEmptyGuess))
return
} else if !wordStore.Validate(c, word) {
c.HTML(http.StatusBadRequest, "error.gohtml", ErrInvalidWord)
errorResponse(c, http.StatusBadRequest, errors.New(ErrInvalidWord))
return
}

err = guessHandlerReply(c, session, word)
if err != nil {
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

if err := session.Save(); err != nil {
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions app/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func HintHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

// Generate the word for the day
h := session.Current()
word, err := wordStore.GetForDay(c, h.DateUser(), session.Mode)
if err != nil {
c.HTML(http.StatusBadRequest, "error.gohtml", err.Error())
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion app/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func IndexHandler(c *gin.Context) {
var err error
data.List, err = listStore.GetList(c, data.Mode)
if err != nil {
c.HTML(http.StatusBadRequest, "error.gohtml", fmt.Sprintf("Could not load list %q: %s", data.Mode, err))
errorResponse(c, http.StatusBadRequest, fmt.Errorf("Could not load list %q: %s", data.Mode, err))
return
}

Expand Down
4 changes: 2 additions & 2 deletions app/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ func ResetHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

if err := session.Clear(); err != nil {
c.HTML(http.StatusInternalServerError, "error.gohtml", err.Error())
errorResponse(c, http.StatusInternalServerError, err)
return
}

Expand Down
21 changes: 11 additions & 10 deletions app/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
_ "embed"
"errors"
"guess_my_word/internal/model"
"log/slog"
"net/http"
Expand All @@ -26,7 +27,7 @@ func StatsHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand All @@ -38,25 +39,25 @@ func StatsHandler(c *gin.Context) {
wordYesterday, err := wordStore.GetForDay(c, dateYesterday, "default")
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}
wordYesterdayHard, err := wordStore.GetForDay(c, dateYesterday, "hard")
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}
wordToday, err := wordStore.GetForDay(c, dateToday, "default")
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}
wordTodayHard, err := wordStore.GetForDay(c, dateToday, "hard")
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand All @@ -77,7 +78,7 @@ func YesterdayHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand All @@ -90,15 +91,15 @@ func YesterdayHandler(c *gin.Context) {

if dateUser.After(cmp) {
slog.Warn("Too early to reveal word", "date", dateUser)
c.HTML(http.StatusBadRequest, "error.gohtml", ErrRevealToday)
errorResponse(c, http.StatusBadRequest, errors.New(ErrRevealToday))
return
}

// Generate the word for the day
word, err := wordStore.GetForDay(c, dateUser, session.Mode)
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand All @@ -111,15 +112,15 @@ func TodayHandler(c *gin.Context) {
session, err := startSession(c)
if err != nil {
slog.Warn("Unable to start session", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

// Generate the word for the day
word, err := wordStore.GetForDay(c, session.DateUser(), session.Mode)
if err != nil {
slog.Warn("Unable to get day", "error", err)
c.HTML(http.StatusBadRequest, "error.gohtml", err)
errorResponse(c, http.StatusBadRequest, err)
return
}

Expand Down
56 changes: 3 additions & 53 deletions app/templates/error.gohtml
Original file line number Diff line number Diff line change
@@ -1,55 +1,5 @@
<!DOCTYPE html>
<html>
{{ template "header.gohtml" . }}

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>Guess My Word</title>
<strong>Error: {{ . }}</strong>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
crossorigin="anonymous">
<link rel="icon" href="/assets/favicon.png" />
<link rel="stylesheet" href="/assets/index.css" />
</head>

<body>
<noscript>
<strong>We're sorry but Guess My Word doesn't work properly without JavaScript enabled.
Please enable it to continue.</strong>
</noscript>

<div id="app">
<div class="container">
<header>
<h1>
<a href="/">
<img src="/assets/logo.svg"
alt="Question by Gregor Cresnar from the Noun Project, https://thenounproject.com/search/?q=question&i=540041" />
</a>
Guess My Word
</h1>
</header>

<div class="row">
<div class="col-md-12">
<p>
This application hosts a daily word game. Guessing using the form
below will place your guess above or below the box based on its place
in the alphabet relative to today's word. Every day a new word will
become guessable. Good luck!
</p>
</div>
</div>

<hr />

<strong>Error: {{ . }}</strong>

<hr />
</div>
</div>
</body>

</html>
{{ template "footer.gohtml" . }}
4 changes: 2 additions & 2 deletions app/templates/header.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link rel="stylesheet" href="/assets/index.css" />
</head>

<body class="mode-{{ .List.Name }}">
<body class="{{ if .List }}mode-{{ .List.Name }}{{ end }}">
<noscript>
<strong>We're sorry but Guess My Word doesn't work properly without JavaScript enabled.
Please enable it to continue.</strong>
Expand Down Expand Up @@ -47,7 +47,7 @@
</div>
</nav>

{{ if .Session.Current.Stale }}
{{ if and .Session .Session.Current.Stale }}
<div class="alert alert-warning" role="alert">
<form method="POST" action="/reset">
A new day has dawned, and a fresh word is available! <button type="submit" class="btn btn-success">Start guessing</button>.
Expand Down

0 comments on commit ed17407

Please sign in to comment.