Use sentry echo integration to send errors

This commit is contained in:
kolaente 2020-06-19 19:07:50 +02:00
parent 562055763a
commit 88b73a47bd
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 19 additions and 11 deletions

View File

@ -64,6 +64,7 @@ import (
"code.vikunja.io/web/handler"
"github.com/asaskevich/govalidator"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
elog "github.com/labstack/gommon/log"
@ -119,6 +120,9 @@ func NewEcho() *echo.Echo {
}))
}
// panic recover
e.Use(middleware.Recover())
if config.ServiceSentryDsn.GetString() != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: config.ServiceSentryDsn.GetString(),
@ -129,23 +133,27 @@ func NewEcho() *echo.Echo {
}
defer sentry.Flush(5 * time.Second)
e.Use(sentryecho.New(sentryecho.Options{
Repanic: true,
}))
e.HTTPErrorHandler = func(err error, c echo.Context) {
// Only capture errors not already handled by echo
if _, ok := err.(*echo.HTTPError); !ok {
sentry.CaptureException(err)
log.Debugf("Error '%s' send to sentry", err.Error())
hub := sentryecho.GetHubFromContext(c)
if hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("url", c.Request().URL)
hub.CaptureException(err)
})
} else {
sentry.CaptureException(err)
log.Debugf("Could not add context for sending error '%s' to sentry", err.Error())
}
log.Debugf("Error '%s' sent to sentry", err.Error())
}
e.DefaultHTTPErrorHandler(err, c)
}
// Maybe we don't need a middleware since echo recovers the error and bubbles it up to the error handler where
// sentry catches it?
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
defer sentry.Recover()
return next(c)
}
})
}
// Validation