diff options
author | 2022-07-12 08:32:20 +0100 | |
---|---|---|
committer | 2022-07-12 08:32:20 +0100 | |
commit | 6934ae378ab5743da80a5995fc74d167502187b1 (patch) | |
tree | 6e3f58498158f1e844504961338eadb18f5a590e /internal/api/errorhandling.go | |
parent | [performance] Add new index to fix slow web profile queries (#706) (diff) | |
download | gotosocial-6934ae378ab5743da80a5995fc74d167502187b1.tar.xz |
[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>
Diffstat (limited to 'internal/api/errorhandling.go')
-rw-r--r-- | internal/api/errorhandling.go | 37 |
1 files changed, 1 insertions, 36 deletions
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 { |