generated from PenaSide/GolangTemplate
error refactoring done
This commit is contained in:
parent
bc3a0daa2b
commit
5fc7161ffe
@ -24,8 +24,6 @@ import (
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
|
||||
)
|
||||
|
||||
// TODO replace all errors.HTTP with api.error to return logging
|
||||
|
||||
const defaultCurrency = "RUB" // TODO move
|
||||
|
||||
type API2 struct {
|
||||
@ -79,6 +77,11 @@ func (api *API2) error(ctx echo.Context, status int, message string, rest ...any
|
||||
})
|
||||
}
|
||||
|
||||
func (api *API2) errorOld(ctx echo.Context, err errors.Error) error {
|
||||
api.logger.Error("error:", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
func (api *API2) noauth(ctx echo.Context) error {
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
}
|
||||
@ -99,7 +102,7 @@ func (api *API2) DeleteAccount(ctx echo.Context) error {
|
||||
|
||||
account, err := api.account.Remove(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -118,7 +121,7 @@ func (api *API2) ChangeAccount(ctx echo.Context) error {
|
||||
|
||||
account, err := api.account.UpdateName(ctx.Request().Context(), userID, request)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -133,7 +136,7 @@ func (api *API2) SetAccountVerificationStatus(ctx echo.Context, userID string) e
|
||||
account, err := api.account.SetStatus(ctx.Request().Context(), userID, request.Status)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to set status on <SetVerificationStatus> of <AccountService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -147,7 +150,7 @@ func (api *API2) GetAccount(ctx echo.Context) error {
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -161,7 +164,7 @@ func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if account != nil {
|
||||
@ -170,12 +173,12 @@ func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
|
||||
user, err := api.clients.auth.GetUser(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
account, err = api.account.Insert(ctx.Request().Context(), &models.Account{UserID: user.ID, Wallet: models.Wallet{Currency: defaultCurrency}})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -184,7 +187,7 @@ func (api *API2) AddAccount(ctx echo.Context) error {
|
||||
func (api *API2) DeleteDirectAccount(ctx echo.Context, userID string) error {
|
||||
account, err := api.account.Remove(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -193,7 +196,7 @@ func (api *API2) DeleteDirectAccount(ctx echo.Context, userID string) error {
|
||||
func (api *API2) GetDirectAccount(ctx echo.Context, userID string) error {
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, account)
|
||||
@ -209,7 +212,7 @@ func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsP
|
||||
|
||||
count, err := api.account.CountAll(ctx.Request().Context())
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
@ -221,7 +224,7 @@ func (api *API2) PaginationAccounts(ctx echo.Context, params PaginationAccountsP
|
||||
|
||||
accounts, err := api.account.FindMany(ctx.Request().Context(), page, limit)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
response := models.PaginationResponse[models.Account]{
|
||||
@ -246,7 +249,7 @@ func (api *API2) RemoveFromCart(ctx echo.Context, params RemoveFromCartParams) e
|
||||
|
||||
cartItems, err := api.account.RemoveItemFromCart(ctx.Request().Context(), userID, params.Id)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, cartItems)
|
||||
@ -271,7 +274,7 @@ func (api *API2) Add2cart(ctx echo.Context, params Add2cartParams) error {
|
||||
|
||||
tariff, err := api.clients.hubadmin.GetTariff(ctx.Request().Context(), token, tariffID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if tariff == nil {
|
||||
@ -280,7 +283,7 @@ func (api *API2) Add2cart(ctx echo.Context, params Add2cartParams) error {
|
||||
|
||||
cartItems, err := api.account.AddItemToCart(ctx.Request().Context(), userID, tariffID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, cartItems)
|
||||
@ -299,14 +302,14 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
api.logger.Info("account for pay", zap.Any("acc", account))
|
||||
|
||||
tariffs, err := api.clients.hubadmin.GetTariffs(ctx.Request().Context(), accessToken, account.Cart)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
api.logger.Info("tariffs for pay", zap.Any("acc", tariffs))
|
||||
@ -324,7 +327,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Date: timestamppb.New(time.Now()),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
api.logger.Info("discountResponse for pay", zap.Any("acc", discount.ApplyDiscountRequest{
|
||||
@ -364,7 +367,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Currency: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
updatedAccount = accountx
|
||||
} else {
|
||||
@ -374,7 +377,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
To: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
accountx, err := api.account.ChangeWallet(ctx.Request().Context(), request.Account.UserID, &models.Wallet{
|
||||
@ -385,7 +388,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Currency: request.Account.Wallet.Currency,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
updatedAccount = accountx
|
||||
@ -397,7 +400,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
Comment: "Успешная оплата корзины",
|
||||
RawDetails: tariffs,
|
||||
}); err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
// TODO: обработать ошибки при отправке сообщений
|
||||
@ -430,7 +433,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
if _, err := api.account.ClearCart(ctx.Request().Context(), account.UserID); err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
updatedAccount.Cart = []string{}
|
||||
@ -443,7 +446,7 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
func (api *API2) GetCurrencies(ctx echo.Context) error {
|
||||
currencyList, err := api.currency.FindCurrenciesList(ctx.Request().Context(), models.DefaultCurrencyListName)
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if err != nil && err.Type() == errors.ErrNotFound {
|
||||
@ -466,7 +469,7 @@ func (api *API2) UpdateCurrencies(ctx echo.Context) error {
|
||||
Currencies: currencies,
|
||||
})
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if err != nil && err.Type() == errors.ErrNotFound {
|
||||
@ -475,7 +478,7 @@ func (api *API2) UpdateCurrencies(ctx echo.Context) error {
|
||||
Currencies: currencies,
|
||||
})
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
return ctx.JSON(http.StatusOK, newCurrencyList.Currencies)
|
||||
}
|
||||
@ -496,7 +499,7 @@ func (api *API2) GetHistory(ctx echo.Context, params GetHistoryParams) error {
|
||||
|
||||
count, err := api.history.CountAll(ctx.Request().Context(), dto)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
@ -508,7 +511,7 @@ func (api *API2) GetHistory(ctx echo.Context, params GetHistoryParams) error {
|
||||
|
||||
histories, err := api.history.FindMany(ctx.Request().Context(), dto)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
returnHistories := models.PaginationResponse[models.History]{
|
||||
@ -533,7 +536,7 @@ func (api *API2) RequestMoney(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
if err := utils.ValidateGetPaymentLinkBody(request); err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
link, err := api.GetPaymentLink(ctx.Request().Context(), &models.GetPaymentLinkRequest{
|
||||
@ -542,7 +545,7 @@ func (api *API2) RequestMoney(ctx echo.Context) error {
|
||||
ClientIP: ctx.RealIP(),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, &models.GetPaymentLinkResponse{Link: link})
|
||||
@ -566,7 +569,7 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
currency := request.Currency
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
cash, err := api.clients.currency.Translate(ctx.Request().Context(), &models.TranslateCurrency{
|
||||
@ -575,7 +578,7 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
To: currency,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
updatedAccount, err := api.account.ChangeWallet(ctx.Request().Context(), account.UserID, &models.Wallet{
|
||||
@ -584,7 +587,7 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
Money: account.Wallet.Money,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
return api.errorOld(ctx, err)
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, updatedAccount)
|
||||
|
Loading…
Reference in New Issue
Block a user