Use sentry echo integration to send errors
This commit is contained in:
parent
562055763a
commit
88b73a47bd
|
@ -64,6 +64,7 @@ import (
|
||||||
"code.vikunja.io/web/handler"
|
"code.vikunja.io/web/handler"
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
|
sentryecho "github.com/getsentry/sentry-go/echo"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
elog "github.com/labstack/gommon/log"
|
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 config.ServiceSentryDsn.GetString() != "" {
|
||||||
if err := sentry.Init(sentry.ClientOptions{
|
if err := sentry.Init(sentry.ClientOptions{
|
||||||
Dsn: config.ServiceSentryDsn.GetString(),
|
Dsn: config.ServiceSentryDsn.GetString(),
|
||||||
|
@ -129,23 +133,27 @@ func NewEcho() *echo.Echo {
|
||||||
}
|
}
|
||||||
defer sentry.Flush(5 * time.Second)
|
defer sentry.Flush(5 * time.Second)
|
||||||
|
|
||||||
|
e.Use(sentryecho.New(sentryecho.Options{
|
||||||
|
Repanic: true,
|
||||||
|
}))
|
||||||
|
|
||||||
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
||||||
// Only capture errors not already handled by echo
|
// Only capture errors not already handled by echo
|
||||||
if _, ok := err.(*echo.HTTPError); !ok {
|
if _, ok := err.(*echo.HTTPError); !ok {
|
||||||
sentry.CaptureException(err)
|
hub := sentryecho.GetHubFromContext(c)
|
||||||
log.Debugf("Error '%s' send to sentry", err.Error())
|
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)
|
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
|
// Validation
|
||||||
|
|
Loading…
Reference in New Issue
Block a user