generated from PenaSide/GolangTemplate
more error cleanups #2
This commit is contained in:
parent
38bb4fb43c
commit
a32f4d5efd
@ -79,6 +79,10 @@ func (api *API2) error(ctx echo.Context, status int, message string, rest ...any
|
||||
})
|
||||
}
|
||||
|
||||
func (api *API2) noauth(ctx echo.Context) error {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
}
|
||||
|
||||
// Health
|
||||
|
||||
func (api *API2) GetHealth(ctx echo.Context) error {
|
||||
@ -90,7 +94,7 @@ func (api *API2) GetHealth(ctx echo.Context) error {
|
||||
func (api *API2) DeleteAccount(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
account, err := api.account.Remove(ctx.Request().Context(), userID)
|
||||
@ -104,7 +108,7 @@ func (api *API2) DeleteAccount(ctx echo.Context) error {
|
||||
func (api *API2) ChangeAccount(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
request, bindErr := echotools.Bind[models.Name](ctx)
|
||||
@ -138,7 +142,7 @@ func (api *API2) SetAccountVerificationStatus(ctx echo.Context, userID string) e
|
||||
func (api *API2) GetAccount(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
@ -152,7 +156,7 @@ func (api *API2) GetAccount(ctx echo.Context) error {
|
||||
func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
@ -233,20 +237,15 @@ func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsP
|
||||
func (api *API2) RemoveFromCart(ctx echo.Context, params RemoveFromCartParams) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert jwt payload to string on <RemoveFromCart>")
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
if validate.IsStringEmpty(params.Id) {
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to remove cart item from user <%s>: empty item id", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "empty item id")
|
||||
}
|
||||
|
||||
cartItems, err := api.account.RemoveItemFromCart(ctx.Request().Context(), userID, params.Id)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to remove item from cart on <RemoveFromCart>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -256,49 +255,31 @@ func (api *API2) RemoveFromCart(ctx echo.Context, params RemoveFromCartParams) e
|
||||
func (api *API2) Add2cart(ctx echo.Context, params Add2cartParams) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert jwt payload to string on <Add2cart>")
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
token, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert access token payload to string on <Add2cart>")
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
if validate.IsStringEmpty(params.Id) {
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to add cart item to user <%s>: empty item id", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "empty item id")
|
||||
}
|
||||
|
||||
request := &models.AddItemToCart{
|
||||
UserID: userID,
|
||||
TariffID: params.Id,
|
||||
AccessToken: token,
|
||||
}
|
||||
tariffID := params.Id
|
||||
|
||||
tariff, err := api.clients.hubadmin.GetTariff(ctx.Request().Context(), request.AccessToken, request.TariffID)
|
||||
tariff, err := api.clients.hubadmin.GetTariff(ctx.Request().Context(), token, tariffID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get tariff on <Add2cart>",
|
||||
zap.Error(err),
|
||||
zap.String("tariffID", request.TariffID),
|
||||
zap.String("accessToken", request.AccessToken),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
if tariff == nil {
|
||||
return errors.HTTP(ctx,
|
||||
errors.New(fmt.Errorf("failed to get tariff <%s> on <Add2cart>: tariff not found", request.TariffID),
|
||||
errors.ErrNotFound,
|
||||
))
|
||||
return api.error(ctx, http.StatusNotFound, "tariff not found")
|
||||
}
|
||||
|
||||
cartItems, err := api.account.AddItemToCart(ctx.Request().Context(), request.UserID, request.TariffID)
|
||||
cartItems, err := api.account.AddItemToCart(ctx.Request().Context(), userID, tariffID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to add item to cart", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -308,19 +289,16 @@ func (api *API2) Add2cart(ctx echo.Context, params Add2cartParams) error {
|
||||
func (api *API2) PayCart(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert jwt payload to string on <Pay> of <CartController>")
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert jwt payload to string", errors.ErrInvalidArgs))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
accessToken, ok := ctx.Get(models.AuthJWTDecodedAccessTokenKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert access token payload to string on <Pay> of <CartController>")
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to convert access token payload to string", errors.ErrInvalidArgs))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to find account on <Pay> of <CartService>", zap.String("userID", userID), zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -328,7 +306,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
|
||||
tariffs, err := api.clients.hubadmin.GetTariffs(ctx.Request().Context(), accessToken, account.Cart)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get tarrifs on <Pay> of <CartService>", zap.Strings("cart", account.Cart), zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -347,7 +324,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Date: timestamppb.New(time.Now()),
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to discount on <Pay> of <CartService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -363,8 +339,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
}))
|
||||
|
||||
if account.Wallet.Money < int64(discountResponse.Price) {
|
||||
api.logger.Error("insufficient funds on <Pay> of <CartService>")
|
||||
return errors.HTTP(ctx, errors.New(fmt.Errorf("insufficient funds: %d", int64(discountResponse.Price)-account.Wallet.Money), errors.ErrInsufficientFunds))
|
||||
return api.error(ctx, http.StatusPaymentRequired, "insufficient funds: %d", int64(discountResponse.Price)-account.Wallet.Money)
|
||||
}
|
||||
|
||||
// WithdrawAccountWalletMoney
|
||||
@ -389,13 +364,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Currency: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to replenish wallet on <ReplenishAccountWallet> of <WalletService>",
|
||||
zap.Error(err),
|
||||
zap.String("Currency", request.Account.Wallet.Currency),
|
||||
zap.Int64("Money", request.Account.Wallet.Money-request.Money),
|
||||
zap.Int64("Cash", request.Account.Wallet.Cash+request.Money),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
updatedAccount = accountx
|
||||
@ -406,7 +374,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
To: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to translate money on <WithdrawAccountWalletMoney> of <WalletService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -418,13 +385,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Currency: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to replenish wallet on <ReplenishAccountWallet> of <WalletService>",
|
||||
zap.Error(err),
|
||||
zap.String("Currency", request.Account.Wallet.Currency),
|
||||
zap.Int64("Money", request.Account.Wallet.Money-request.Money),
|
||||
zap.Int64("Cash", request.Account.Wallet.Cash+cash),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -437,7 +397,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Comment: "Успешная оплата корзины",
|
||||
RawDetails: tariffs,
|
||||
}); err != nil {
|
||||
api.logger.Error("failed to insert history on <Pay> of <CartService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user