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
}
type errorTpl struct {
type msgTpl struct {
publicTpl
ErrorTitle string
ErrorMessage string
MessageTitle string
Message string
}
var (
@ -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.`))
}
@ -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.`))
}
@ -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."))
}

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
}
// 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
}