From be053980f2b1dd9129a007907a29f4f4da07647f Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Fri, 18 Nov 2022 23:36:49 -0600 Subject: [PATCH] add stack trace on 500 --- internal/auth/auth.go | 4 ++++ internal/echoerror/middleware.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 59763f1..1a04d33 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -13,6 +13,7 @@ import ( "github.com/alexedwards/argon2id" "github.com/eternal-flame-AD/yoake/config" "github.com/eternal-flame-AD/yoake/internal/echoerror" + "github.com/eternal-flame-AD/yoake/internal/util" "github.com/gorilla/sessions" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -125,6 +126,9 @@ func Middleware(store sessions.Store) echo.MiddlewareFunc { } else if auth.Valid { auth.Roles = existingRoles } + if util.Contain(auth.Roles, string(RoleAdmin)) { + c.Set("devel", true) + } c.Set("auth_"+AuthSessionName, auth) c.Set("auth_store", store) diff --git a/internal/echoerror/middleware.go b/internal/echoerror/middleware.go index f024d09..0b626c6 100644 --- a/internal/echoerror/middleware.go +++ b/internal/echoerror/middleware.go @@ -5,6 +5,7 @@ import ( "fmt" "html/template" "net/http" + "runtime/debug" "github.com/labstack/echo/v4" ) @@ -80,6 +81,15 @@ var ( func Middleware(errorWriter ErrorWriter) func(next echo.HandlerFunc) echo.HandlerFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { + defer func() { + if err := recover(); err != nil { + if c.Get("devel") == true { + errorWriter(c, fmt.Errorf("panic: %v; stacktrace: %s", err, string(debug.Stack()))) + } else { + errorWriter(c, fmt.Errorf("panic: %v", err)) + } + } + }() err := next(c) if err != nil { if errEcho, ok := err.(*echo.HTTPError); ok {