From 199288045480856985e377f7475a1991a2decc33 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 19 Nov 2020 10:09:05 -0500 Subject: Add theme-specific error page --- cmd/alps/main.go | 3 --- plugins/base/routes.go | 2 +- renderer.go | 13 +++++++++---- server.go | 26 +++++++++++++++++++++----- themes/alps/assets/style.css | 13 ++++++++++++- themes/alps/error.html | 14 ++++++++++++++ themes/alps/nav.html | 2 ++ 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 themes/alps/error.html diff --git a/cmd/alps/main.go b/cmd/alps/main.go index fc1fdd8..b013701 100644 --- a/cmd/alps/main.go +++ b/cmd/alps/main.go @@ -70,9 +70,6 @@ func main() { e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ Format: "${time_rfc3339} method=${method}, uri=${uri}, status=${status}\n", })) - } - - if options.Debug { e.Logger.SetLevel(log.DEBUG) } diff --git a/plugins/base/routes.go b/plugins/base/routes.go index 8698a3a..2107abb 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -118,7 +118,7 @@ func newIMAPBaseRenderData(ctx *alps.Context, } if mboxName != "" { if active, err = getMailboxStatus(c, mboxName); err != nil { - return err + return echo.NewHTTPError(http.StatusNotFound, err) } } if mboxName == "INBOX" { diff --git a/renderer.go b/renderer.go index 4875745..1123b0b 100644 --- a/renderer.go +++ b/renderer.go @@ -70,14 +70,19 @@ type RenderData interface { // BaseRenderData: *alps.NewBaseRenderData(ctx), // // other fields... // } -func NewBaseRenderData(ctx *Context) *BaseRenderData { +func NewBaseRenderData(ectx echo.Context) *BaseRenderData { + ctx, isactx := ectx.(*Context) + global := GlobalRenderData{ Extra: make(map[string]interface{}), - Path: strings.Split(ctx.Request().URL.Path, "/")[1:], + Path: strings.Split(ectx.Request().URL.Path, "/")[1:], Title: "Webmail", - URL: ctx.Request().URL, + URL: ectx.Request().URL, HavePlugin: func(name string) bool { + if !isactx { + return false + } for _, plugin := range ctx.Server.plugins { if plugin.Name() == name { return true @@ -87,7 +92,7 @@ func NewBaseRenderData(ctx *Context) *BaseRenderData { }, } - if ctx.Session != nil { + if isactx && ctx.Session != nil { global.LoggedIn = true global.Username = ctx.Session.username } diff --git a/server.go b/server.go index 7159697..b1ac3a1 100644 --- a/server.go +++ b/server.go @@ -382,15 +382,31 @@ func New(e *echo.Echo, options *Options) (*Server, error) { return nil, err } - e.HTTPErrorHandler = func(err error, c echo.Context) { + e.HTTPErrorHandler = func(err error, ctx echo.Context) { code := http.StatusInternalServerError if he, ok := err.(*echo.HTTPError); ok { code = he.Code - } else { - c.Logger().Error(err) } - // TODO: hide internal errors - c.String(code, err.Error()) + + type ErrorRenderData struct { + BaseRenderData + Code int + Err error + Status string + } + rdata := ErrorRenderData{ + BaseRenderData: *NewBaseRenderData(ctx), + Err: err, + Code: code, + Status: http.StatusText(code), + } + + if err := ctx.Render(code, "error.html", &rdata); err != nil { + ctx.Logger().Error(fmt.Errorf( + "Error occured rendering error page: %w. How meta.", err)) + } + + ctx.Logger().Error(err) } e.Pre(func(next echo.HandlerFunc) echo.HandlerFunc { diff --git a/themes/alps/assets/style.css b/themes/alps/assets/style.css index 27d8463..e07f4e2 100644 --- a/themes/alps/assets/style.css +++ b/themes/alps/assets/style.css @@ -123,8 +123,19 @@ footer { text-align: right; } .actions { padding: 0.5rem; } -.container { flex: 1 auto; display: flex; flex-direction: column; flex-wrap: nowrap; min-width: 0; } +.container { + flex: 1 auto; + display: flex; + flex-direction: column; + flex-wrap: nowrap; + min-width: 0; +} +.container.error { + max-width: 800px; + margin: 0 auto; + padding: 1rem 0; +} aside { flex: 0 0 180px; } diff --git a/themes/alps/error.html b/themes/alps/error.html new file mode 100644 index 0000000..2e25f60 --- /dev/null +++ b/themes/alps/error.html @@ -0,0 +1,14 @@ +{{template "head.html" .}} + +
+
+

{{.Code}}: {{.Status}}

+

+ An error occured. You can try + returning to your inbox, + or contact support. +

+
+
+ +{{template "foot.html"}} diff --git a/themes/alps/nav.html b/themes/alps/nav.html index d14a862..d7eee9c 100644 --- a/themes/alps/nav.html +++ b/themes/alps/nav.html @@ -22,10 +22,12 @@ {{ end }} >Contacts {{ end }} + {{ if .GlobalData.LoggedIn }}
{{ .GlobalData.Username }} Settings Sign Out
+ {{ end }} -- cgit v1.2.3