diff --git a/handlers/handlers.go b/handlers/handlers.go index ba3ae67..4ff392e 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -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 diff --git a/handlers/helper.go b/handlers/helper.go new file mode 100644 index 0000000..a8787c7 --- /dev/null +++ b/handlers/helper.go @@ -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 +} diff --git a/handlers/page.go b/handlers/page.go index d36e068..4b43e04 100644 --- a/handlers/page.go +++ b/handlers/page.go @@ -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 } diff --git a/templates/500.tmpl b/templates/500.tmpl deleted file mode 100644 index 0794877..0000000 --- a/templates/500.tmpl +++ /dev/null @@ -1,5 +0,0 @@ -{{ template "head.tmpl" . }} - -

Webservice currently unavailable 500

An unexpected condition was encountered.
Our service team has been dispatched to bring it back online.

- -{{ template "footer.tmpl" . }} diff --git a/templates/errorPage.tmpl b/templates/errorPage.tmpl new file mode 100644 index 0000000..07e7ef3 --- /dev/null +++ b/templates/errorPage.tmpl @@ -0,0 +1,21 @@ +{{ template "head.tmpl" . }} + +{{ template "navbar.tmpl" . }} +
+
+
+

+ {{ .Status }} - {{ .StatusText }} +

+ {{ if .DevelMode }} +

+ Error: {{ .Error }}
+

+ {{ end }} + +
+
+
+{{ template "footer.tmpl" . }}