Update error page

- Add retry button for GET requests
- Only show signin button when it's a session error
This commit is contained in:
r 2021-04-03 06:39:06 +00:00
parent 089d4ac500
commit 2cb6a515ac
4 changed files with 23 additions and 10 deletions

View File

@ -34,7 +34,9 @@ type NavData struct {
type ErrorData struct { type ErrorData struct {
*CommonData *CommonData
Error string Err string
Retry bool
SessionErr bool
} }
type HomePageData struct { type HomePageData struct {

View File

@ -109,15 +109,21 @@ func (s *service) cdata(c *client, title string, count int, rinterval int,
return return
} }
func (s *service) ErrorPage(c *client, err error) error { func (s *service) ErrorPage(c *client, err error, retry bool) error {
var errStr string var errStr string
var sessionErr bool
if err != nil { if err != nil {
errStr = err.Error() errStr = err.Error()
if err == errInvalidSession || err == errInvalidCSRFToken {
sessionErr = true
}
} }
cdata := s.cdata(nil, "error", 0, 0, "") cdata := s.cdata(nil, "error", 0, 0, "")
data := &renderer.ErrorData{ data := &renderer.ErrorData{
CommonData: cdata, CommonData: cdata,
Error: errStr, Err: errStr,
Retry: retry,
SessionErr: sessionErr,
} }
return s.renderer.Render(c.rctx, c.w, renderer.ErrorPage, data) return s.renderer.Render(c.rctx, c.w, renderer.ErrorPage, data)
} }

View File

@ -62,11 +62,11 @@ func redirect(c *client, url string) {
func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
r := mux.NewRouter() r := mux.NewRouter()
writeError := func(c *client, err error, t int) { writeError := func(c *client, err error, t int, retry bool) {
switch t { switch t {
case HTML: case HTML:
c.w.WriteHeader(http.StatusInternalServerError) c.w.WriteHeader(http.StatusInternalServerError)
s.ErrorPage(c, err) s.ErrorPage(c, err, retry)
case JSON: case JSON:
c.w.WriteHeader(http.StatusInternalServerError) c.w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(c.w).Encode(map[string]string{ json.NewEncoder(c.w).Encode(map[string]string{
@ -110,13 +110,13 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler {
err = authenticate(c, at) err = authenticate(c, at)
if err != nil { if err != nil {
writeError(c, err, rt) writeError(c, err, rt, req.Method == http.MethodGet)
return return
} }
err = f(c) err = f(c)
if err != nil { if err != nil {
writeError(c, err, rt) writeError(c, err, rt, req.Method == http.MethodGet)
return return
} }
} }

View File

@ -2,10 +2,15 @@
{{template "header.tmpl" (WithContext .CommonData $.Ctx)}} {{template "header.tmpl" (WithContext .CommonData $.Ctx)}}
<div class="page-title"> Error </div> <div class="page-title"> Error </div>
<div class="error-text"> {{.Error}} </div> <div class="error-text"> {{.Err}} </div>
<div> <div>
<a href="/timeline/home">Home</a> <a href="/timeline/home">home</a>
<a href="/signin" target="_top">Sign In</a> {{if .Retry}}
<a href="{{$.Ctx.Referrer}}">retry</a>
{{end}}
{{if .SessionErr}}
<a href="/signin" target="_top">signin</a>
{{end}}
</div> </div>
{{template "footer.tmpl"}} {{template "footer.tmpl"}}