Refactor "error" view to a generic "message"

This commit is contained in:
Kailash Nadh 2019-07-18 12:44:46 +05:30
parent 81d3046374
commit 7d9758c3f5
3 changed files with 28 additions and 17 deletions

View File

@ -41,10 +41,10 @@ type unsubTpl struct {
Blacklist bool Blacklist bool
} }
type errorTpl struct { type msgTpl struct {
publicTpl publicTpl
ErrorTitle string MessageTitle string
ErrorMessage string Message string
} }
var ( var (
@ -79,8 +79,8 @@ func handleUnsubscribePage(c echo.Context) error {
if !regexValidUUID.MatchString(campUUID) || if !regexValidUUID.MatchString(campUUID) ||
!regexValidUUID.MatchString(subUUID) { !regexValidUUID.MatchString(subUUID) {
return c.Render(http.StatusBadRequest, "error", return c.Render(http.StatusBadRequest, "message",
makeErrorTpl("Invalid request", "", makeMsgTpl("Invalid request", "",
`The unsubscription request contains invalid IDs. `The unsubscription request contains invalid IDs.
Please follow the correct link.`)) Please follow the correct link.`))
} }
@ -97,8 +97,8 @@ func handleUnsubscribePage(c echo.Context) error {
if !blacklist { if !blacklist {
num, _ := res.RowsAffected() num, _ := res.RowsAffected()
if num == 0 { if num == 0 {
return c.Render(http.StatusBadRequest, "error", return c.Render(http.StatusBadRequest, "message",
makeErrorTpl("Already unsubscribed", "", makeMsgTpl("Already unsubscribed", "",
`You are not subscribed to this mailing list. `You are not subscribed to this mailing list.
You may have already unsubscribed.`)) You may have already unsubscribed.`))
} }
@ -119,15 +119,15 @@ func handleLinkRedirect(c echo.Context) error {
if !regexValidUUID.MatchString(linkUUID) || if !regexValidUUID.MatchString(linkUUID) ||
!regexValidUUID.MatchString(campUUID) || !regexValidUUID.MatchString(campUUID) ||
!regexValidUUID.MatchString(subUUID) { !regexValidUUID.MatchString(subUUID) {
return c.Render(http.StatusBadRequest, "error", return c.Render(http.StatusBadRequest, "message",
makeErrorTpl("Invalid link", "", "The link you clicked is invalid.")) makeMsgTpl("Invalid link", "", "The link you clicked is invalid."))
} }
var url string var url string
if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil { if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil {
app.Logger.Printf("error fetching redirect link: %s", err) app.Logger.Printf("error fetching redirect link: %s", err)
return c.Render(http.StatusInternalServerError, "error", return c.Render(http.StatusInternalServerError, "message",
makeErrorTpl("Error opening link", "", makeMsgTpl("Error opening link", "",
"There was an error opening the link. Please try later.")) "There was an error opening the link. Please try later."))
} }

View File

@ -0,0 +1,10 @@
{{ define "message" }}
{{ template "header" .}}
<h2>{{ .Data.Title }}</h2>
<div>
{{ .Data.Message }}
</div>
{{ template "footer" .}}
{{ end }}

View File

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