Skip to content

Commit

Permalink
Refactor "error" view to a generic "message"
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jul 18, 2019
1 parent 81d3046 commit 7d9758c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
22 changes: 11 additions & 11 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ type unsubTpl struct {
Blacklist bool
}

type errorTpl struct {
type msgTpl struct {
publicTpl
ErrorTitle string
ErrorMessage string
MessageTitle string
Message string
}

var (
Expand Down Expand Up @@ -79,8 +79,8 @@ func handleUnsubscribePage(c echo.Context) error {

if !regexValidUUID.MatchString(campUUID) ||
!regexValidUUID.MatchString(subUUID) {
return c.Render(http.StatusBadRequest, "error",
makeErrorTpl("Invalid request", "",
return c.Render(http.StatusBadRequest, "message",
makeMsgTpl("Invalid request", "",
`The unsubscription request contains invalid IDs.
Please follow the correct link.`))
}
Expand All @@ -97,8 +97,8 @@ func handleUnsubscribePage(c echo.Context) error {
if !blacklist {
num, _ := res.RowsAffected()
if num == 0 {
return c.Render(http.StatusBadRequest, "error",
makeErrorTpl("Already unsubscribed", "",
return c.Render(http.StatusBadRequest, "message",
makeMsgTpl("Already unsubscribed", "",
`You are not subscribed to this mailing list.
You may have already unsubscribed.`))
}
Expand All @@ -119,15 +119,15 @@ func handleLinkRedirect(c echo.Context) error {
if !regexValidUUID.MatchString(linkUUID) ||
!regexValidUUID.MatchString(campUUID) ||
!regexValidUUID.MatchString(subUUID) {
return c.Render(http.StatusBadRequest, "error",
makeErrorTpl("Invalid link", "", "The link you clicked is invalid."))
return c.Render(http.StatusBadRequest, "message",
makeMsgTpl("Invalid link", "", "The link you clicked is invalid."))
}

var url string
if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil {
app.Logger.Printf("error fetching redirect link: %s", err)
return c.Render(http.StatusInternalServerError, "error",
makeErrorTpl("Error opening link", "",
return c.Render(http.StatusInternalServerError, "message",
makeMsgTpl("Error opening link", "",
"There was an error opening the link. Please try later."))
}

Expand Down
10 changes: 10 additions & 0 deletions public/templates/message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ define "message" }}
{{ template "header" .}}

<h2>{{ .Data.Title }}</h2>
<div>
{{ .Data.Message }}
</div>

{{ template "footer" .}}
{{ end }}
13 changes: 7 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,17 @@ func normalizeTags(tags []string) []string {
return out
}

// makeErrorTpl takes error details and returns an errorTpl
// with the error details applied to be rendered in an HTML view.
func makeErrorTpl(pageTitle, heading, desc string) errorTpl {
// makeMsgTpl takes a page title, heading, and message and returns
// a msgTpl that can be rendered as a HTML view. This is used for
// rendering aribtrary HTML views with error and success messages.
func makeMsgTpl(pageTitle, heading, msg string) msgTpl {
if heading == "" {
heading = pageTitle
}
err := errorTpl{}
err := msgTpl{}
err.Title = pageTitle
err.ErrorTitle = heading
err.ErrorMessage = desc
err.MessageTitle = heading
err.Message = msg

return err
}
Expand Down

0 comments on commit 7d9758c

Please sign in to comment.