From 6934ae378ab5743da80a5995fc74d167502187b1 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Tue, 12 Jul 2022 08:32:20 +0100 Subject: [chore] improved router logging, recovery and error handling (#705) * move panic recovery to logging middleware, improve logging + panic recovery logic Signed-off-by: kim * remove dead code Signed-off-by: kim * remove skip paths code Signed-off-by: kim * re-enable log quoting Signed-off-by: kim * use human-friendly bytesize in logging body size Signed-off-by: kim * only disable quoting in debug builds Signed-off-by: kim * use logrus level instead of debug.DEBUG() to enable/disable quoting Signed-off-by: kim * shutup linter Signed-off-by: kim * fix instance tests Signed-off-by: kim * fix gin test contexts created with missing engine HTML renderer Signed-off-by: kim * add note regarding not logging query parameters Signed-off-by: kim * better explain 'DisableQuoting' logic Signed-off-by: kim * add license text Signed-off-by: kim --- internal/api/errorhandling.go | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) (limited to 'internal/api/errorhandling.go') diff --git a/internal/api/errorhandling.go b/internal/api/errorhandling.go index 834f49ee8..32699fe36 100644 --- a/internal/api/errorhandling.go +++ b/internal/api/errorhandling.go @@ -22,7 +22,6 @@ import ( "context" "net/http" - "codeberg.org/gruf/go-errors/v2" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -86,47 +85,13 @@ func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context, d // if something goes wrong during the function, it will recover and just try to serve // an appropriate application/json content-type error. func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context, domain string) (*apimodel.Instance, gtserror.WithCode)) { - path := c.Request.URL.Path - if raw := c.Request.URL.RawQuery; raw != "" { - path = path + "?" + raw - } - - l := logrus.WithFields(logrus.Fields{ - "path": path, - "error": errWithCode.Error(), - }) - - statusCode := errWithCode.Code() - - if statusCode == http.StatusInternalServerError { - l.Error("Internal Server Error") - } else { - l.Debug("handling error") - } - - // if we panic for any reason during error handling, - // we should still try to return a basic code - defer func() { - if p := recover(); p != nil { - // Fetch stacktrace up to this point - callers := errors.GetCallers(3, 10) - - // Log this panic to the standard log - l = l.WithField("stacktrace", callers) - l.Errorf("recovered from panic: %v", p) - - // Respond with determined error code - c.JSON(statusCode, gin.H{"error": errWithCode.Safe()}) - } - }() - // discover if we're allowed to serve a nice html error page, // or if we should just use a json. Normally we would want to // check for a returned error, but if an error occurs here we // can just fall back to default behavior (serve json error). accept, _ := NegotiateAccept(c, HTMLOrJSONAcceptHeaders...) - if statusCode == http.StatusNotFound { + if errWithCode.Code() == http.StatusNotFound { // use our special not found handler with useful status text NotFoundHandler(c, instanceGet, accept) } else { -- cgit v1.2.3