mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-22 15:14:51 +01:00
Add ignoreErrors to sentry
This commit is contained in:
@@ -44,7 +44,7 @@ func (controller *AuthController) Auth(c echo.Context) error {
|
|||||||
|
|
||||||
accessToken, refreshToken, err := controller.svc.GenerateToken(c.Request().Context(), body.Login, body.Password, body.RefreshToken)
|
accessToken, refreshToken, err := controller.svc.GenerateToken(c.Request().Context(), body.Login, body.Password, body.RefreshToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusBadRequest, responses.BadAuthError)
|
return c.JSON(http.StatusUnauthorized, responses.BadAuthError)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, &AuthResponseBody{
|
return c.JSON(http.StatusOK, &AuthResponseBody{
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package responses
|
package responses
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
@@ -44,7 +43,7 @@ func HTTPErrorHandler(err error, c echo.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Logger().Error(err)
|
c.Logger().Error(err)
|
||||||
if hub := sentryecho.GetHubFromContext(c); hub != nil && isErrAllowedForSentry(err) {
|
if hub := sentryecho.GetHubFromContext(c); hub != nil {
|
||||||
hub.WithScope(func(scope *sentry.Scope) {
|
hub.WithScope(func(scope *sentry.Scope) {
|
||||||
scope.SetExtra("UserID", c.Get("UserID"))
|
scope.SetExtra("UserID", c.Get("UserID"))
|
||||||
hub.CaptureException(err)
|
hub.CaptureException(err)
|
||||||
@@ -59,32 +58,3 @@ func HTTPErrorHandler(err error, c echo.Context) {
|
|||||||
}
|
}
|
||||||
// TODO: use an error matching the error code
|
// TODO: use an error matching the error code
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is a simple way to try to convert err.Message interface
|
|
||||||
// to ErrorResponse without external packages
|
|
||||||
func errToErrorResponse(err error) *ErrorResponse {
|
|
||||||
httpError, ok := err.(*echo.HTTPError)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
responseJson, err := json.Marshal(httpError.Message)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
errorResponse := &ErrorResponse{}
|
|
||||||
err = json.Unmarshal(responseJson, errorResponse)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return errorResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
// currently only bad auth errors are not allowed
|
|
||||||
func isErrAllowedForSentry(err error) bool {
|
|
||||||
errResponse := errToErrorResponse(err)
|
|
||||||
|
|
||||||
return errResponse == nil || errResponse.Code != BadAuthError.Code
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package responses
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBadAuthErrorsNotAllowedForSentry(t *testing.T) {
|
|
||||||
badAuthErrResponse := echo.NewHTTPError(http.StatusBadRequest, echo.Map{
|
|
||||||
"error": true,
|
|
||||||
"code": 1,
|
|
||||||
"message": "bad auth",
|
|
||||||
})
|
|
||||||
|
|
||||||
isAllowed := isErrAllowedForSentry(badAuthErrResponse)
|
|
||||||
assert.False(t, isAllowed)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNotBadAuthErrorsAllowedForSentry(t *testing.T) {
|
|
||||||
notBadAuthErrResponse := echo.NewHTTPError(http.StatusBadRequest, echo.Map{
|
|
||||||
"error": true,
|
|
||||||
"code": 2,
|
|
||||||
"message": "not bad auth",
|
|
||||||
})
|
|
||||||
|
|
||||||
isAllowed := isErrAllowedForSentry(notBadAuthErrResponse)
|
|
||||||
assert.True(t, isAllowed)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNonErrorResponseErrorsAllowedForSentry(t *testing.T) {
|
|
||||||
err := errors.New("random error")
|
|
||||||
|
|
||||||
isAllowed := isErrAllowedForSentry(err)
|
|
||||||
assert.True(t, isAllowed)
|
|
||||||
}
|
|
||||||
@@ -25,7 +25,7 @@ func Middleware(secret []byte) echo.MiddlewareFunc {
|
|||||||
config.SigningKey = secret
|
config.SigningKey = secret
|
||||||
config.ErrorHandlerWithContext = func(err error, c echo.Context) error {
|
config.ErrorHandlerWithContext = func(err error, c echo.Context) error {
|
||||||
c.Logger().Error(err)
|
c.Logger().Error(err)
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, echo.Map{
|
return echo.NewHTTPError(http.StatusUnauthorized, echo.Map{
|
||||||
"error": true,
|
"error": true,
|
||||||
"code": 1,
|
"code": 1,
|
||||||
"message": "bad auth",
|
"message": "bad auth",
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -94,6 +94,7 @@ func main() {
|
|||||||
if c.SentryDSN != "" {
|
if c.SentryDSN != "" {
|
||||||
if err = sentry.Init(sentry.ClientOptions{
|
if err = sentry.Init(sentry.ClientOptions{
|
||||||
Dsn: c.SentryDSN,
|
Dsn: c.SentryDSN,
|
||||||
|
IgnoreErrors: []string{"401"},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
logger.Errorf("sentry init error: %v", err)
|
logger.Errorf("sentry init error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user