DRY up error responses

This commit is contained in:
Michael Bumann
2022-01-22 20:38:14 +01:00
parent 1359d97d6a
commit 891dd8394c
5 changed files with 49 additions and 60 deletions

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"
)
@@ -29,29 +30,17 @@ func (controller *AddInvoiceController) AddInvoice(c echo.Context) error {
if err := c.Bind(&body); err != nil {
c.Logger().Errorf("Failed to load addinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if err := c.Validate(&body); err != nil {
c.Logger().Errorf("Invalid addinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
amount, err := controller.svc.ParseInt(body.Amount)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
c.Logger().Infof("Adding invoice: user_id=%v memo=%s value=%v description_hash=%s", userID, body.Memo, amount, body.DescriptionHash)
@@ -59,11 +48,7 @@ func (controller *AddInvoiceController) AddInvoice(c echo.Context) error {
if err != nil {
c.Logger().Errorf("Error creating invoice: %v", err)
// TODO: sentry notification
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
var responseBody struct {

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"
)
@@ -33,20 +34,13 @@ func (controller *AuthController) Auth(c echo.Context) error {
}
if err := c.Validate(&body); err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
accessToken, refreshToken, err := controller.svc.GenerateToken(body.Login, body.Password, body.RefreshToken)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": err.Error(),
})
c.Logger().Infof("Generate Token error %v", err)
return c.JSON(http.StatusBadRequest, responses.BadAuthError)
}
return c.JSON(http.StatusOK, echo.Map{

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,11 +27,7 @@ func (controller *CheckPaymentController) CheckPayment(c echo.Context) error {
// Probably we did not find the invoice
if err != nil {
c.Logger().Errorf("Invalid checkpayment request payment_hash=%s", rHash)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
var responseBody struct {

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"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"
)
@@ -29,30 +30,19 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
if err := c.Bind(&reqBody); err != nil {
c.Logger().Errorf("Failed to load payinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if err := c.Validate(&reqBody); err != nil {
c.Logger().Errorf("Invalid payinvoice request body: %v", err)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
paymentRequest := reqBody.Invoice
decodedPaymentRequest, err := controller.svc.DecodePaymentRequest(paymentRequest)
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 8,
"message": "Bad arguments",
})
c.Logger().Errorf("Invalid payment request: %v", err)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
// TODO: zero amount invoices
/*
@@ -70,11 +60,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
if err != nil {
c.Logger().Errorf("Error creating invoice: %v", err)
// TODO: sentry notification
return c.JSON(http.StatusInternalServerError, echo.Map{
"error": true,
"code": 6,
"message": "Something went wrong. Please try again later",
})
return c.JSON(http.StatusInternalServerError, responses.GeneralServerError)
}
currentBalance, err := controller.svc.CurrentUserBalance(context.TODO(), userID)
@@ -85,11 +71,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
if currentBalance < invoice.Amount {
c.Logger().Errorf("User does not have enough balance invoice_id=%v user_id=%v balance=%v amount=%v", invoice.ID, userID, currentBalance, invoice.Amount)
return c.JSON(http.StatusBadRequest, echo.Map{
"error": true,
"code": 2,
"message": fmt.Sprintf("not enough balance (%v). Make sure you have at least 1%% reserved for potential fees", currentBalance),
})
return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError)
}
sendPaymentResponse, err := controller.svc.PayInvoice(invoice)

31
lib/responses/errors.go Normal file
View File

@@ -0,0 +1,31 @@
package responses
type ErrorResponse struct {
Error bool `json:"error"`
Code int `json:"code"`
Message string `json:"message"`
}
var GeneralServerError = ErrorResponse{
Error: true,
Code: 6,
Message: "Something went wrong. Please try again later",
}
var BadArgumentsError = ErrorResponse{
Error: true,
Code: 8,
Message: "Bad arguments",
}
var BadAuthError = ErrorResponse{
Error: true,
Code: 1,
Message: "bad auth",
}
var NotEnoughBalanceError = ErrorResponse{
Error: true,
Code: 2,
Message: "not enough balance. Make sure you have at least 1%% reserved for potential fees",
}