Custom HTTP error handler and send sentry notifications

This commit is contained in:
Michael Bumann
2022-01-24 23:37:03 +01:00
parent 9b292398da
commit d10701ad2f
7 changed files with 32 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
)
@@ -47,7 +48,7 @@ func (controller *AddInvoiceController) AddInvoice(c echo.Context) error {
invoice, err := controller.svc.AddIncomingInvoice(userID, amount, body.Memo, body.DescriptionHash)
if err != nil {
c.Logger().Errorf("Error creating invoice: %v", err)
// TODO: sentry notification
sentry.CaptureException(err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}

View File

@@ -39,8 +39,7 @@ func (controller *AuthController) Auth(c echo.Context) error {
accessToken, refreshToken, err := controller.svc.GenerateToken(body.Login, body.Password, body.RefreshToken)
if err != nil {
c.Logger().Infof("Generate Token error %v", err)
return c.JSON(http.StatusBadRequest, responses.BadAuthError)
return err
}
return c.JSON(http.StatusOK, echo.Map{

View File

@@ -8,6 +8,7 @@ import (
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getsentry/sentry-go"
"github.com/labstack/echo/v4"
)
@@ -42,6 +43,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
decodedPaymentRequest, err := controller.svc.DecodePaymentRequest(paymentRequest)
if err != nil {
c.Logger().Errorf("Invalid payment request: %v", err)
sentry.CaptureException(err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
// TODO: zero amount invoices
@@ -58,9 +60,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
invoice, err := controller.svc.AddOutgoingInvoice(userID, paymentRequest, decodedPaymentRequest)
if err != nil {
c.Logger().Errorf("Error creating invoice: %v", err)
// TODO: sentry notification
return c.JSON(http.StatusInternalServerError, responses.GeneralServerError)
return err
}
currentBalance, err := controller.svc.CurrentUserBalance(context.TODO(), userID)
@@ -77,7 +77,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
sendPaymentResponse, err := controller.svc.PayInvoice(invoice)
if err != nil {
c.Logger().Errorf("Payment failed: %v", err)
// TODO: sentry notification
sentry.CaptureException(err)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 10,

View File

@@ -1,5 +1,13 @@
package responses
import (
"net/http"
"github.com/getsentry/sentry-go"
sentryecho "github.com/getsentry/sentry-go/echo"
"github.com/labstack/echo/v4"
)
type ErrorResponse struct {
Error bool `json:"error"`
Code int `json:"code"`
@@ -29,3 +37,17 @@ var NotEnoughBalanceError = ErrorResponse{
Code: 2,
Message: "not enough balance. Make sure you have at least 1%% reserved for potential fees",
}
func HTTPErrorHandler(err error, c echo.Context) {
if c.Response().Committed {
return
}
c.Logger().Error(err)
if hub := sentryecho.GetHubFromContext(c); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetExtra("UserID", c.Get("UserID"))
hub.CaptureException(err)
})
}
c.JSON(http.StatusInternalServerError, GeneralServerError)
}

View File

@@ -124,7 +124,6 @@ func (svc *LndhubService) SendPaymentSync(tx *bun.Tx, invoice *models.Invoice) (
// If there was a payment error we rollback and return an error
if sendPaymentResult.GetPaymentError() != "" || sendPaymentResult.GetPaymentPreimage() == nil {
// TODO: log the error / sentry?
return sendPaymentResponse, errors.New(sendPaymentResult.GetPaymentError())
}

View File

@@ -118,8 +118,8 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
// receive the next invoice update
rawInvoice, err := invoiceSubscriptionStream.Recv()
if err != nil {
// TODO: sentry notification
svc.Logger.Errorf("Error processing invoice update subscription: %v", err)
sentry.CaptureException(err)
// TODO: close the stream somehoe before retrying?
// Wait 30 seconds and try to reconnect
// TODO: implement some backoff

View File

@@ -15,6 +15,7 @@ import (
"github.com/getAlby/lndhub.go/db"
"github.com/getAlby/lndhub.go/db/migrations"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
@@ -68,6 +69,7 @@ func main() {
e := echo.New()
e.HideBanner = true
e.HTTPErrorHandler = responses.HTTPErrorHandler
e.Validator = &lib.CustomValidator{Validator: validator.New()}
e.Use(middleware.Recover())