chore: add missing logs

This commit is contained in:
im-adithya
2023-09-20 13:53:58 +05:30
parent 6925458980
commit 4ff3ebf79b
12 changed files with 51 additions and 18 deletions

View File

@@ -50,6 +50,7 @@ func AddInvoice(c echo.Context, svc *service.LndhubService, userID int64) error
amount, err := svc.ParseInt(body.Amount)
if err != nil || amount < 0 {
c.Logger().Errorf("Invalid amount %v for user_id:%v error: %v", amount, userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
@@ -63,7 +64,8 @@ func AddInvoice(c echo.Context, svc *service.LndhubService, userID int64) error
if svc.Config.MaxAccountBalance > 0 {
currentBalance, err := svc.CurrentUserBalance(c.Request().Context(), userID)
if err != nil {
return c.JSON(http.StatusBadRequest, responses.GeneralServerError)
c.Logger().Errorf("Error fetching balance for user_id:%v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if currentBalance+amount > svc.Config.MaxAccountBalance {
c.Logger().Errorf("Max account balance exceeded for user_id:%v (balance:%v + amount:%v)", userID, currentBalance, amount)

View File

@@ -49,6 +49,7 @@ func (controller *AuthController) Auth(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if err := c.Validate(&body); err != nil {
c.Logger().Errorf("Failed to validate auth user request body: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
@@ -56,7 +57,8 @@ func (controller *AuthController) Auth(c echo.Context) error {
// To support Swagger we also look in the Form data
params, err := c.FormParams()
if err != nil {
return err
c.Logger().Errorf("Failed to get form parameters: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
login := params.Get("login")
password := params.Get("password")
@@ -69,8 +71,10 @@ 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 {
if err.Error() == responses.AccountDeactivatedError.Message {
c.Logger().Errorf("Account Deactivated for user: %s", body.Login)
return c.JSON(http.StatusUnauthorized, responses.AccountDeactivatedError)
}
c.Logger().Errorf("Authentication error for user: %s error: %v", body.Login, err)
return c.JSON(http.StatusUnauthorized, responses.BadAuthError)
}

View File

@@ -3,6 +3,7 @@ package controllers
import (
"net/http"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/labstack/echo/v4"
)
@@ -26,7 +27,8 @@ func (controller *BalanceController) Balance(c echo.Context) error {
userId := c.Get("UserID").(int64)
balance, err := controller.svc.CurrentUserBalance(c.Request().Context(), userId)
if err != nil {
return err
c.Logger().Errorf("Failed to retrieve user balance for user id: %v error: %v", userId, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
return c.JSON(http.StatusOK, &BalanceResponse{
BTC: struct{ AvailableBalance int64 }{

View File

@@ -3,6 +3,7 @@ package controllers
import (
"net/http"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/labstack/echo/v4"
)
@@ -77,7 +78,8 @@ func (controller *GetInfoController) GetInfo(c echo.Context) error {
info, err := controller.svc.GetInfo(c.Request().Context())
if err != nil {
return err
c.Logger().Errorf("Failed to retrieve info: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if controller.svc.Config.CustomName != "" {
info.Alias = controller.svc.Config.CustomName

View File

@@ -5,6 +5,7 @@ import (
"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/labstack/echo/v4"
)
@@ -51,7 +52,8 @@ func (controller *GetTXSController) GetTXS(c echo.Context) error {
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, common.InvoiceTypeOutgoing)
if err != nil {
return err
c.Logger().Errorf("Failed to retrieve transactions: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
response := make([]OutgoingInvoice, len(invoices))
@@ -78,7 +80,8 @@ func (controller *GetTXSController) GetUserInvoices(c echo.Context) error {
invoices, err := controller.svc.InvoicesFor(c.Request().Context(), userId, common.InvoiceTypeIncoming)
if err != nil {
return err
c.Logger().Errorf("Failed to retrieve invoices for user_id: %v error: %v", userId, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
response := make([]IncomingInvoice, len(invoices))

View File

@@ -9,6 +9,7 @@ import (
"net/url"
"strings"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
@@ -66,7 +67,8 @@ func (controller *HomeController) QR(c echo.Context) error {
url := fmt.Sprintf("bluewallet:setlndhuburl?url=%s", encoded)
png, err := qrcode.Encode(url, qrcode.Medium, 256)
if err != nil {
return err
c.Logger().Errorf("Error encoding QR: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
return c.Blob(http.StatusOK, "image/png", png)
}
@@ -74,16 +76,19 @@ func (controller *HomeController) QR(c echo.Context) error {
func (controller *HomeController) Home(c echo.Context) error {
info, err := controller.svc.GetInfo(c.Request().Context())
if err != nil {
return err
c.Logger().Errorf("Failed to retrieve info: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
channels, err := controller.svc.LndClient.ListChannels(c.Request().Context(), &lnrpc.ListChannelsRequest{})
if err != nil {
return err
c.Logger().Errorf("Failed to list channels: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
tmpl, err := template.New("index").Parse(controller.html)
if err != nil {
return err
c.Logger().Errorf("Failed to parse template: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
// See original code: https://github.com/BlueWallet/LndHub/blob/master/controllers/website.js#L32
maxChanCapacity := -1
@@ -117,7 +122,8 @@ func (controller *HomeController) Home(c echo.Context) error {
var buf bytes.Buffer
err = tmpl.Execute(&buf, content)
if err != nil {
return err
c.Logger().Errorf("Failed to parse template: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
c.Response().Header().Set(echo.HeaderCacheControl, "public, max-age=300, stale-if-error=21600") // cache for 5 minutes or if error for 6 hours max
return c.HTMLBlob(http.StatusOK, buf.Bytes())

View File

@@ -83,7 +83,8 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
if err != nil {
return err
c.Logger().Errorf("Failed to check balance user_id: %v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if !ok {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
@@ -91,7 +92,8 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
}
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, "", lnPayReq)
if err != nil {
return err
c.Logger().Errorf("Error saving invoice user_id: %v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if _, err := hex.DecodeString(invoice.DestinationPubkeyHex); err != nil || len(invoice.DestinationPubkeyHex) != common.DestinationPubkeyHexSize {
c.Logger().Errorf("Invalid destination pubkey hex user_id:%v pubkey:%v", userID, len(invoice.DestinationPubkeyHex))
@@ -101,6 +103,7 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
for key, value := range reqBody.CustomRecords {
intKey, err := strconv.Atoi(key)
if err != nil {
c.Logger().Errorf("Invalid custom records user_id: %v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
invoice.DestinationCustomRecords[uint64(intKey)] = []byte(value)

View File

@@ -77,6 +77,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
if decodedPaymentRequest.NumSatoshis == 0 {
amt, err := controller.svc.ParseInt(reqBody.Amount)
if err != nil || amt <= 0 {
c.Logger().Errorf("Invalid amount in payment request user_id:%v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
lnPayReq.PayReq.NumSatoshis = amt
@@ -91,7 +92,8 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
if err != nil {
return err
c.Logger().Errorf("Error checking user balance user_id:%v error: %v", userID, err)
return c.JSON(http.StatusBadRequest, responses.GeneralServerError)
}
if !ok {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
@@ -100,7 +102,8 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, paymentRequest, lnPayReq)
if err != nil {
return err
c.Logger().Errorf("Error saving invoice user_id:%v payment_request:%v", userID, paymentRequest)
return c.JSON(http.StatusBadRequest, responses.GeneralServerError)
}
sendPaymentResponse, err := controller.svc.PayInvoice(c.Request().Context(), invoice)
if err != nil {