generated from PenaSide/GolangTemplate
more error cleanups #3
This commit is contained in:
parent
13c955d65f
commit
8226f2cbaa
@ -24,7 +24,7 @@ import (
|
||||
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/validate"
|
||||
)
|
||||
|
||||
// TODO update echo errors
|
||||
// TODO replace all errors.HTTP with api.error to return logging
|
||||
|
||||
const defaultCurrency = "RUB" // TODO move
|
||||
|
||||
@ -413,7 +413,6 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
defer waitGroup.Done()
|
||||
|
||||
if err := api.producer.Send(ctx.Request().Context(), userID, ¤tTariff); err != nil {
|
||||
api.logger.Error("failed to send tariff on <Send> of <TariffBrokerService>", zap.Error(err))
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
sendErrors = append(sendErrors, err)
|
||||
@ -427,11 +426,10 @@ func (api *API2) PayCart(ctx echo.Context) error {
|
||||
for _, err := range sendErrors {
|
||||
api.logger.Error("failed to send tariffs to broker on <Pay> of <CartService>", zap.Error(err))
|
||||
}
|
||||
return errors.HTTP(ctx, errors.NewWithMessage("failed to send tariffs to broker", errors.ErrInternalError))
|
||||
return api.error(ctx, http.StatusInternalServerError, "failed to send tariffs to broker")
|
||||
}
|
||||
|
||||
if _, err := api.account.ClearCart(ctx.Request().Context(), account.UserID); err != nil {
|
||||
api.logger.Error("failed to clear cart on <Pay> of <CartService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -445,10 +443,6 @@ 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 {
|
||||
api.logger.Error(
|
||||
"failed to get currencies on <GetCurrencies> of <CurrencyService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -462,31 +456,16 @@ func (api *API2) GetCurrencies(ctx echo.Context) error {
|
||||
func (api *API2) UpdateCurrencies(ctx echo.Context) error {
|
||||
currenciesPtr, bindErr := echotools.Bind[[]string](ctx)
|
||||
if bindErr != nil {
|
||||
api.logger.Error(
|
||||
"failed to parse body on <PutCurrencies> of <CurrencyController>",
|
||||
zap.Error(bindErr),
|
||||
)
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to parse body: %w", bindErr),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "faild to bind currencies")
|
||||
}
|
||||
|
||||
currencies := *currenciesPtr
|
||||
|
||||
if len(currencies) < 1 {
|
||||
currencies = make([]string, 0) // TODO WHY?
|
||||
}
|
||||
|
||||
currencyList, err := api.currency.ReplaceCurrencies(ctx.Request().Context(), &models.CurrencyList{
|
||||
Name: models.DefaultCurrencyListName,
|
||||
Currencies: currencies,
|
||||
})
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
api.logger.Error(
|
||||
"failed to put currencies on <PutCurrencies> of <CurrencyService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -496,10 +475,6 @@ func (api *API2) UpdateCurrencies(ctx echo.Context) error {
|
||||
Currencies: currencies,
|
||||
})
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
api.logger.Error(
|
||||
"failed to insert new currency list on <PutCurrencies> of <CurrencyService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
return ctx.JSON(http.StatusOK, newCurrencyList.Currencies)
|
||||
@ -521,30 +496,24 @@ func (api *API2) GetHistory(ctx echo.Context, params GetHistoryParams) error {
|
||||
|
||||
count, err := api.history.CountAll(ctx.Request().Context(), dto)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to count histories on <GetHistoryList> of <HistoryService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
var returnHistories models.PaginationResponse[models.History]
|
||||
if count == 0 {
|
||||
returnHistories = models.PaginationResponse[models.History]{TotalPages: 0, Records: []models.History{}}
|
||||
} else {
|
||||
totalPages := int64(math.Ceil(float64(count) / float64(dto.Pagination.Limit)))
|
||||
returnHistories := models.PaginationResponse[models.History]{TotalPages: 0, Records: []models.History{}}
|
||||
return ctx.JSON(http.StatusOK, returnHistories)
|
||||
}
|
||||
|
||||
histories, err := api.history.FindMany(ctx.Request().Context(), dto)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get historiy list on <GetHistoryList> of <HistoryService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
totalPages := int64(math.Ceil(float64(count) / float64(dto.Pagination.Limit)))
|
||||
|
||||
returnHistories = models.PaginationResponse[models.History]{
|
||||
TotalPages: totalPages,
|
||||
Records: histories,
|
||||
}
|
||||
histories, err := api.history.FindMany(ctx.Request().Context(), dto)
|
||||
if err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
returnHistories := models.PaginationResponse[models.History]{
|
||||
TotalPages: totalPages,
|
||||
Records: histories,
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, returnHistories)
|
||||
@ -555,26 +524,16 @@ func (api *API2) GetHistory(ctx echo.Context, params GetHistoryParams) error {
|
||||
func (api *API2) RequestMoney(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert jwt payload to string on <GetPaymentLink> of <WallerController>")
|
||||
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
request, bindErr := echotools.Bind[models.GetPaymentLinkBody](ctx)
|
||||
if bindErr != nil {
|
||||
api.logger.Error("failed to bind body on <GetPaymentLink> of <WalletController>", zap.Error(bindErr))
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to parse body on <GetPaymentLink> of <WalletController>: %w", bindErr),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "faild to bind payment link")
|
||||
}
|
||||
|
||||
if validateErr := utils.ValidateGetPaymentLinkBody(request); validateErr != nil {
|
||||
api.logger.Error("failed to validate body on <GetPaymentLink> of <WalletController>", zap.Error(validateErr))
|
||||
return errors.HTTP(ctx, validateErr)
|
||||
if err := utils.ValidateGetPaymentLinkBody(request); err != nil {
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
link, err := api.GetPaymentLink(ctx.Request().Context(), &models.GetPaymentLinkRequest{
|
||||
@ -583,7 +542,6 @@ func (api *API2) RequestMoney(ctx echo.Context) error {
|
||||
ClientIP: ctx.RealIP(),
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get payment link on <GetPaymentLink> of <WalletController>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -593,38 +551,21 @@ func (api *API2) RequestMoney(ctx echo.Context) error {
|
||||
func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||
if !ok {
|
||||
api.logger.Error("failed to convert jwt payload to string on <ChangeCurrency> of <WallerController>")
|
||||
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.noauth(ctx)
|
||||
}
|
||||
|
||||
request, bindErr := echotools.Bind[models.ChangeCurrency](ctx)
|
||||
if bindErr != nil {
|
||||
api.logger.Error("failed to bind body on <ChangeCurrency> of <WalletController>", zap.Error(bindErr))
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to parse body on <ChangeCurrency> of <WalletController>: %w", bindErr),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "faild to bind currency")
|
||||
}
|
||||
|
||||
if validate.IsStringEmpty(request.Currency) {
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("empty currency key on <ChangeCurrency> of <WalletController>: %w", errors.ErrInvalidArgs),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "empty currency")
|
||||
}
|
||||
|
||||
currency := request.Currency
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to find account on <ChangeCurrency> of <WalletService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
zap.Any("currency", currency),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -634,7 +575,6 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
To: currency,
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to translate currency on <ChangeCurrency> of <WalletService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -644,7 +584,6 @@ func (api *API2) ChangeCurrency(ctx echo.Context) error {
|
||||
Money: account.Wallet.Money,
|
||||
})
|
||||
if err != nil {
|
||||
api.logger.Error("failed to update wallet on <ChangeCurrency> of <WalletService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user