handlers: update error handling
All checks were successful
continuous-integration/drone/push Build is passing

* have a common error page tmpl
This commit is contained in:
leo 2023-04-21 12:33:14 +02:00
parent b1c926befd
commit 555bc65502
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
5 changed files with 134 additions and 22 deletions

@ -286,7 +286,17 @@ func Signup() echo.HandlerFunc {
)
if err != nil {
log.Warnf("error: %q", err)
http.Error(c.Response().Writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
err = renderErrorPage(
c.Response().Writer,
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
err.Error(),
)
if err != nil {
return err
}
}
return nil
@ -353,15 +363,25 @@ func Home() echo.HandlerFunc {
if err != nil {
if err == http.ErrNoCookie {
log.Infof("error no cookie: %q", err)
http.Error(c.Response().Writer, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
err = renderErrorPage(
c.Response().Writer,
http.StatusNotFound, http.StatusText(http.StatusNotFound),
err.Error(),
)
if err != nil {
c.Logger().Errorf("error: %q", err)
return err
}
return nil
}
log.Infof("error: %q", err)
http.Error(c.Response().Writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
c.Logger().Errorf("error: %q", err)
return nil
return echo.NewHTTPError(http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
}
if session != nil {
@ -370,7 +390,22 @@ func Home() echo.HandlerFunc {
if err := session.Valid(); err != nil {
log.Warn("invalid or expired session?")
return echo.NewHTTPError(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
c.Logger().Errorf("error: %q", err)
err = renderErrorPage(
c.Response().Writer,
http.StatusNotFound, http.StatusText(http.StatusNotFound),
err.Error(),
)
if err != nil {
c.Logger().Errorf("error: %q", err)
return echo.NewHTTPError(
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
)
}
}
username = session.Value
@ -395,7 +430,22 @@ func Home() echo.HandlerFunc {
if err != nil {
log.Warnf("error: %q", err)
return echo.NewHTTPError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
c.Logger().Errorf("error: %q", err)
err = renderErrorPage(
c.Response().Writer,
http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError),
err.Error(),
)
if err != nil {
c.Logger().Errorf("error: %q", err)
return echo.NewHTTPError(
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
)
}
}
return nil
@ -413,9 +463,9 @@ func Logout() echo.HandlerFunc {
return c.Redirect(302, "/signin")
}
log.Warnf("error: %q", err)
c.Logger().Errorf("error: %q", err)
return nil
return err
}
var username string
@ -451,7 +501,20 @@ func Logout() echo.HandlerFunc {
if err != nil {
log.Warnf("error: %q", err)
return echo.NewHTTPError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
c.Logger().Errorf("error: %q", err)
err = renderErrorPage(
c.Response().Writer,
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError),
err.Error(),
)
if err != nil {
c.Logger().Errorf("error: %q", err)
return err
}
}
return nil

30
handlers/helper.go Normal file

@ -0,0 +1,30 @@
package handlers
import (
"fmt"
"io"
"strconv"
)
func renderErrorPage(wr io.Writer, status int, statusText, error string) error {
tpl := getTmpl("errorPage.tmpl")
strStatus := strconv.Itoa(status)
err := tpl.Execute(wr,
page{
AppName: conf.AppName,
AppVer: appver,
Title: fmt.Sprintf("Error %s - %s", strStatus, statusText),
DevelMode: conf.DevelMode,
Current: strStatus,
Error: error,
Status: strStatus,
StatusText: statusText,
},
)
if err != nil {
return err
}
return nil
}

@ -1,11 +1,14 @@
package handlers
type page struct {
AppName string
AppVer string
Title string
Name string
CSRF string
DevelMode bool
Current string
AppName string
AppVer string
Title string
Name string
CSRF string
DevelMode bool
Current string
Error string
Status string
StatusText string
}

@ -1,5 +0,0 @@
{{ template "head.tmpl" . }}
<body>
<div class="cover"><h1>Webservice currently unavailable <small>500</small></h1><p class="lead">An unexpected condition was encountered.<br />Our service team has been dispatched to bring it back online.</p></div>
{{ template "footer.tmpl" . }}

21
templates/errorPage.tmpl Normal file

@ -0,0 +1,21 @@
{{ template "head.tmpl" . }}
<body class="h-screen bg-white dark:bg-gray-900">
{{ template "navbar.tmpl" . }}
<main class="grow">
<div class="container mx-auto md:items-center text-center">
<div class="place-items-center text-center">
<h1 class="p-2 mt-10 text-4xl font-bold text-purple-700 dark:text-purple-400">
{{ .Status }} - {{ .StatusText }}
</h1>
{{ if .DevelMode }}
<p class="p-2 mt-4 text-xl font-medium text-red-500">
Error: {{ .Error }}<br />
</p>
{{ end }}
<div class="w-1/2 md:w-full px-auto py-3 mx-auto text-sm font-medium tracking-wide text-blue-300 dark:text-blue-500 transition-colors duration-300 hover:underline">
<a href="/">Get back home</a>
</div>
</div>
</div>
</main>
{{ template "footer.tmpl" . }}