mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-23 07:35:04 +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)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, responses.BadAuthError)
|
||||
return c.JSON(http.StatusUnauthorized, responses.BadAuthError)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, &AuthResponseBody{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
@@ -44,7 +43,7 @@ func HTTPErrorHandler(err error, c echo.Context) {
|
||||
return
|
||||
}
|
||||
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) {
|
||||
scope.SetExtra("UserID", c.Get("UserID"))
|
||||
hub.CaptureException(err)
|
||||
@@ -59,32 +58,3 @@ func HTTPErrorHandler(err error, c echo.Context) {
|
||||
}
|
||||
// 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.ErrorHandlerWithContext = func(err error, c echo.Context) error {
|
||||
c.Logger().Error(err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, echo.Map{
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, echo.Map{
|
||||
"error": true,
|
||||
"code": 1,
|
||||
"message": "bad auth",
|
||||
|
||||
Reference in New Issue
Block a user