generated from PenaSide/GolangTemplate
more error cleanups
This commit is contained in:
parent
9a369e5784
commit
cf6046d2fe
@ -52,22 +52,12 @@ func (receiver *AuthClient) GetUser(ctx context.Context, userID string) (*models
|
||||
Headers: map[string]string{"Content-Type": "application/json"},
|
||||
})
|
||||
if err != nil {
|
||||
receiver.logger.Error("failed to request get user on <GetUser> of <AuthClient>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
|
||||
return nil, errors.New(
|
||||
fmt.Errorf("failed to request get user with <%s> on <GetUser> of <AuthClient>: %w", userID, err),
|
||||
errors.ErrInternalError,
|
||||
)
|
||||
}
|
||||
if response.Error != nil {
|
||||
receiver.logger.Error("failed request on <GetUser> of <AuthClient>",
|
||||
zap.String("error", response.Error.Message),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
|
||||
return nil, errors.New(
|
||||
fmt.Errorf("failed request with <%s> on <GetUser> of <AuthClient>: %s", userID, response.Error.Message),
|
||||
utils.DetermineClientErrorResponse(response.StatusCode),
|
||||
|
@ -104,19 +104,16 @@ 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 {
|
||||
api.logger.Error("failed to convert jwt payload to string on <UpdateAccountName> of <AccountController>")
|
||||
return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs))
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
}
|
||||
|
||||
request, bindErr := echotools.Bind[models.Name](ctx)
|
||||
if bindErr != nil {
|
||||
api.logger.Error("failed to bind json request on <UpdateAccountName> of <AccountController>", zap.Error(bindErr))
|
||||
return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to bind json: %w", bindErr), errors.ErrInternalError))
|
||||
return api.error(ctx, http.StatusBadRequest, "failed to bind json: %w", bindErr)
|
||||
}
|
||||
|
||||
account, err := api.account.UpdateName(ctx.Request().Context(), userID, request)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to update account name on <UpdateAccountName> of <AccountService>", zap.Error(err))
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -126,8 +123,7 @@ func (api *API2) ChangeAccount(ctx echo.Context) error {
|
||||
func (api *API2) SetAccountVerificationStatus(ctx echo.Context, userID string) error {
|
||||
request, bindErr := echotools.Bind[models.SetAccountStatus](ctx)
|
||||
if bindErr != nil {
|
||||
api.logger.Error("failed to bind json request on <SetVerificationStatus> of <AccountController>", zap.Error(bindErr))
|
||||
return errors.HTTP(ctx, errors.New(fmt.Errorf("failed to bind json: %w", bindErr), errors.ErrInternalError))
|
||||
return api.error(ctx, http.StatusBadRequest, "failed to bind json: %w", bindErr)
|
||||
}
|
||||
|
||||
account, err := api.account.SetStatus(ctx.Request().Context(), userID, request.Status)
|
||||
@ -142,20 +138,11 @@ 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 {
|
||||
api.logger.Error("failed to convert jwt payload to string on <GetAccount> of <AccountController>")
|
||||
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
}
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get account by id on <GetAccountByUserID> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
@ -165,40 +152,20 @@ 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 {
|
||||
api.logger.Error("failed to convert jwt payload to string on <CreateAccount> of <AccountController>")
|
||||
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||||
errors.ErrInvalidArgs,
|
||||
))
|
||||
return api.error(ctx, http.StatusUnauthorized, "failed to get jwt payload")
|
||||
}
|
||||
|
||||
account, err := api.account.FindByUserID(ctx.Request().Context(), userID)
|
||||
if err != nil && err.Type() != errors.ErrNotFound {
|
||||
api.logger.Error("failed to find account on <CreateAccountByUserID> of <AccountService>",
|
||||
zap.Error(err),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
if account != nil {
|
||||
return errors.HTTP(ctx, errors.New(
|
||||
fmt.Errorf("failed to create account with <%s> on <CreateAccountByUserID> of <AccountService>: %w",
|
||||
userID,
|
||||
errors.ErrConflict,
|
||||
),
|
||||
errors.ErrConflict,
|
||||
))
|
||||
return api.error(ctx, http.StatusBadRequest, "account exists")
|
||||
}
|
||||
|
||||
user, err := api.clients.auth.GetUser(ctx.Request().Context(), userID)
|
||||
if err != nil {
|
||||
api.logger.Error("failed to get user on <CreateAccountByUserID> of <AccountService>",
|
||||
zap.Error(err),
|
||||
zap.String("userID", userID),
|
||||
)
|
||||
|
||||
return errors.HTTP(ctx, err)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user