Combine error templates.

This commit is contained in:
mikestefanello 2021-12-11 16:06:28 -05:00
parent 3a9f0d1879
commit 58e75cf7a6
8 changed files with 24 additions and 18 deletions

View File

@ -21,4 +21,5 @@ up:
.PHONY: run
run:
clear
go run main.go

View File

@ -1,7 +1,6 @@
package controllers
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
@ -26,8 +25,7 @@ func (e *Error) Get(err error, c echo.Context) {
p := NewPage(c)
p.Layout = "main"
p.Title = http.StatusText(code)
// TODO: fallback if there is no error template
p.Name = fmt.Sprintf("errors/%d", code)
p.Name = "error"
p.StatusCode = code
if err = e.RenderPage(c, p); err != nil {

View File

@ -41,6 +41,7 @@ type Page struct {
MaxAge time.Duration
Tags []string
}
RequestID string
}
func NewPage(c echo.Context) Page {
@ -51,6 +52,7 @@ func NewPage(c echo.Context) Page {
StatusCode: http.StatusOK,
Pager: pager.NewPager(c, DefaultItemsPerPage),
Headers: make(map[string]string),
RequestID: c.Response().Header().Get(echo.HeaderXRequestID),
}
p.IsHome = p.Path == "/"

View File

@ -28,12 +28,12 @@
<section class="section">
<div class="container">
{{- if .Title}}
<h1 class="title">{{.Title}}</h1>
{{- end}}
{{- if .Title}}
<h1 class="title">{{.Title}}</h1>
{{- end}}
{{template "messages" .}}
{{template "content" .}}
{{template "messages" .}}
{{template "content" .}}
</div>
</section>
</body>

11
views/pages/error.gohtml Normal file
View File

@ -0,0 +1,11 @@
{{define "content"}}
{{if gt .StatusCode 500}}
<p>Please try again. Request ID: {{.RequestID}}</p>
{{else if or (eq .StatusCode 403) (eq .StatusCode 401)}}
<p>You are not authorized to view the requested page.</p>
{{else if eq .StatusCode 404}}
<p>Click {{link (call .Reverse "home") "here" .Path}} to return home</p>
{{else}}
<p>Something went wrong</p>
{{end}}
{{end}}

View File

@ -1,3 +0,0 @@
{{define "content"}}
<p>Click {{link (call .Reverse "home") "here" .Path}} to return home</p>
{{end}}

View File

@ -1,3 +0,0 @@
{{define "content"}}
<p>Please try again</p>
{{end}}

View File

@ -1,15 +1,15 @@
{{define "content"}}
<form method="post">
<div class="field">
<label for="" class="label">Username</label>
<label for="username" class="label">Username</label>
<div class="control">
<input type="textfield" name="username" class="input" value="{{.Data.Username}}" required>
<input type="text" id="username" name="username" class="input" value="{{.Data.Username}}" required>
</div>
</div>
<div class="field">
<label for="" class="label">Password</label>
<label for="password" class="label">Password</label>
<div class="control">
<input type="password" name="password" placeholder="*******" class="input" required>
<input type="password" id="password" name="password" placeholder="*******" class="input" required>
</div>
</div>
<div class="field is-grouped">