mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] improved router logging, recovery and error handling (#705)
* move panic recovery to logging middleware, improve logging + panic recovery logic Signed-off-by: kim <grufwub@gmail.com> * remove dead code Signed-off-by: kim <grufwub@gmail.com> * remove skip paths code Signed-off-by: kim <grufwub@gmail.com> * re-enable log quoting Signed-off-by: kim <grufwub@gmail.com> * use human-friendly bytesize in logging body size Signed-off-by: kim <grufwub@gmail.com> * only disable quoting in debug builds Signed-off-by: kim <grufwub@gmail.com> * use logrus level instead of debug.DEBUG() to enable/disable quoting Signed-off-by: kim <grufwub@gmail.com> * shutup linter Signed-off-by: kim <grufwub@gmail.com> * fix instance tests Signed-off-by: kim <grufwub@gmail.com> * fix gin test contexts created with missing engine HTML renderer Signed-off-by: kim <grufwub@gmail.com> * add note regarding not logging query parameters Signed-off-by: kim <grufwub@gmail.com> * better explain 'DisableQuoting' logic Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user