mirror of https://gitee.com/answerdev/answer.git
feat: change the original single form error return to multiple form error return
This commit is contained in:
parent
83ad1a185a
commit
8084870096
|
@ -14,35 +14,37 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
|
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
|
||||||
CommentNotFound = "error.comment.not_found"
|
CommentNotFound = "error.comment.not_found"
|
||||||
QuestionNotFound = "error.question.not_found"
|
QuestionNotFound = "error.question.not_found"
|
||||||
AnswerNotFound = "error.answer.not_found"
|
AnswerNotFound = "error.answer.not_found"
|
||||||
CommentEditWithoutPermission = "error.comment.edit_without_permission"
|
CommentEditWithoutPermission = "error.comment.edit_without_permission"
|
||||||
DisallowVote = "error.object.disallow_vote"
|
DisallowVote = "error.object.disallow_vote"
|
||||||
DisallowFollow = "error.object.disallow_follow"
|
DisallowFollow = "error.object.disallow_follow"
|
||||||
DisallowVoteYourSelf = "error.object.disallow_vote_your_self"
|
DisallowVoteYourSelf = "error.object.disallow_vote_your_self"
|
||||||
CaptchaVerificationFailed = "error.object.captcha_verification_failed"
|
CaptchaVerificationFailed = "error.object.captcha_verification_failed"
|
||||||
UserNotFound = "error.user.not_found"
|
OldPasswordVerificationFailed = "error.object.old_password_verification_failed"
|
||||||
UsernameInvalid = "error.user.username_invalid"
|
NewPasswordSameAsPreviousSetting = "error.object.new_password_same_as_previous_setting"
|
||||||
UsernameDuplicate = "error.user.username_duplicate"
|
UserNotFound = "error.user.not_found"
|
||||||
UserSetAvatar = "error.user.set_avatar"
|
UsernameInvalid = "error.user.username_invalid"
|
||||||
EmailDuplicate = "error.email.duplicate"
|
UsernameDuplicate = "error.user.username_duplicate"
|
||||||
EmailVerifyURLExpired = "error.email.verify_url_expired"
|
UserSetAvatar = "error.user.set_avatar"
|
||||||
EmailNeedToBeVerified = "error.email.need_to_be_verified"
|
EmailDuplicate = "error.email.duplicate"
|
||||||
UserSuspended = "error.user.suspended"
|
EmailVerifyURLExpired = "error.email.verify_url_expired"
|
||||||
ObjectNotFound = "error.object.not_found"
|
EmailNeedToBeVerified = "error.email.need_to_be_verified"
|
||||||
TagNotFound = "error.tag.not_found"
|
UserSuspended = "error.user.suspended"
|
||||||
TagNotContainSynonym = "error.tag.not_contain_synonym_tags"
|
ObjectNotFound = "error.object.not_found"
|
||||||
RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition"
|
TagNotFound = "error.tag.not_found"
|
||||||
ThemeNotFound = "error.theme.not_found"
|
TagNotContainSynonym = "error.tag.not_contain_synonym_tags"
|
||||||
LangNotFound = "error.lang.not_found"
|
RankFailToMeetTheCondition = "error.rank.fail_to_meet_the_condition"
|
||||||
ReportHandleFailed = "error.report.handle_failed"
|
ThemeNotFound = "error.theme.not_found"
|
||||||
ReportNotFound = "error.report.not_found"
|
LangNotFound = "error.lang.not_found"
|
||||||
ReadConfigFailed = "error.config.read_config_failed"
|
ReportHandleFailed = "error.report.handle_failed"
|
||||||
DatabaseConnectionFailed = "error.database.connection_failed"
|
ReportNotFound = "error.report.not_found"
|
||||||
InstallCreateTableFailed = "error.database.create_table_failed"
|
ReadConfigFailed = "error.config.read_config_failed"
|
||||||
InstallConfigFailed = "error.install.create_config_failed"
|
DatabaseConnectionFailed = "error.database.connection_failed"
|
||||||
SiteInfoNotFound = "error.site_info.not_found"
|
InstallCreateTableFailed = "error.database.create_table_failed"
|
||||||
RecommendTagNotExist = "error.tag.recommend_tag_not_found"
|
InstallConfigFailed = "error.install.create_config_failed"
|
||||||
|
SiteInfoNotFound = "error.site_info.not_found"
|
||||||
|
RecommendTagNotExist = "error.tag.recommend_tag_not_found"
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/answerdev/answer/internal/base/middleware"
|
"github.com/answerdev/answer/internal/base/middleware"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service"
|
"github.com/answerdev/answer/internal/service"
|
||||||
"github.com/answerdev/answer/internal/service/action"
|
"github.com/answerdev/answer/internal/service/action"
|
||||||
|
@ -106,24 +107,22 @@ func (uc *UserController) UserEmailLogin(ctx *gin.Context) {
|
||||||
|
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := uc.userService.EmailLogin(ctx, req)
|
resp, err := uc.userService.EmailLogin(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "e_mail",
|
ErrorField: "e_mail",
|
||||||
Value: reason.EmailOrPasswordWrong,
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.EmailOrPasswordWrong),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.EmailOrPasswordWrong), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
uc.actionService.ActionRecordDel(ctx, schema.ActionRecordTypeLogin, ctx.ClientIP())
|
||||||
|
@ -146,12 +145,11 @@ func (uc *UserController) RetrievePassWord(ctx *gin.Context) {
|
||||||
}
|
}
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeFindPass, ctx.ClientIP())
|
||||||
|
@ -277,12 +275,11 @@ func (uc *UserController) UserVerifyEmailSend(ctx *gin.Context) {
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(),
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(),
|
||||||
req.CaptchaID, req.CaptchaCode)
|
req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -314,22 +311,19 @@ func (uc *UserController) UserModifyPassWord(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !oldPassVerification {
|
if !oldPassVerification {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "old_pass",
|
ErrorField: "old_pass",
|
||||||
Value: "error.object.old_password_verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.OldPasswordVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.OldPasswordVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.OldPass == req.Pass {
|
if req.OldPass == req.Pass {
|
||||||
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
ErrorField: "pass",
|
||||||
Key: "pass",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.NewPasswordSameAsPreviousSetting),
|
||||||
Value: "error.object.new_password_same_as_previous_setting",
|
})
|
||||||
}
|
handler.HandleResponse(ctx, errors.BadRequest(reason.NewPasswordSameAsPreviousSetting), errFields)
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = uc.userService.UserModifyPassword(ctx, req)
|
err = uc.userService.UserModifyPassword(ctx, req)
|
||||||
|
@ -502,19 +496,18 @@ func (uc *UserController) UserChangeEmailSendCode(ctx *gin.Context) {
|
||||||
|
|
||||||
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
captchaPass := uc.actionService.ActionRecordVerifyCaptcha(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP(), req.CaptchaID, req.CaptchaCode)
|
||||||
if !captchaPass {
|
if !captchaPass {
|
||||||
resp := schema.UserVerifyEmailErrorResponse{
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
||||||
Key: "captcha_code",
|
ErrorField: "captcha_code",
|
||||||
Value: "error.object.verification_failed",
|
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||||
}
|
})
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), resp)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
_, _ = uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
||||||
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
|
resp, err := uc.userService.UserChangeEmailSendCode(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
resp.Value = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.Value)
|
resp.ErrorMsg = translator.GlobalTrans.Tr(handler.GetLang(ctx), resp.ErrorMsg)
|
||||||
}
|
}
|
||||||
handler.HandleResponse(ctx, err, resp)
|
handler.HandleResponse(ctx, err, resp)
|
||||||
return
|
return
|
||||||
|
|
|
@ -417,8 +417,3 @@ type UserVerifyEmailSendReq struct {
|
||||||
CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"`
|
CaptchaID string `validate:"omitempty,gt=0,lte=500" json:"captcha_id"`
|
||||||
CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"`
|
CaptchaCode string `validate:"omitempty,gt=0,lte=500" json:"captcha_code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserVerifyEmailErrorResponse struct {
|
|
||||||
Key string `json:"key"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/Chain-Zhang/pinyin"
|
"github.com/Chain-Zhang/pinyin"
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
"github.com/answerdev/answer/internal/base/validator"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service/activity"
|
"github.com/answerdev/answer/internal/service/activity"
|
||||||
|
@ -481,7 +482,7 @@ func (us *UserService) encryptPassword(ctx context.Context, Pass string) (string
|
||||||
|
|
||||||
// UserChangeEmailSendCode user change email verification
|
// UserChangeEmailSendCode user change email verification
|
||||||
func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
|
func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.UserChangeEmailSendCodeReq) (
|
||||||
resp *schema.UserVerifyEmailErrorResponse, err error) {
|
resp *validator.FormErrorField, err error) {
|
||||||
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
|
userInfo, exist, err := us.userRepo.GetByUserID(ctx, req.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -495,9 +496,9 @@ func (us *UserService) UserChangeEmailSendCode(ctx context.Context, req *schema.
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
resp = &schema.UserVerifyEmailErrorResponse{
|
resp = &validator.FormErrorField{
|
||||||
Key: "e_mail",
|
ErrorField: "e_mail",
|
||||||
Value: reason.EmailDuplicate,
|
ErrorMsg: reason.EmailDuplicate,
|
||||||
}
|
}
|
||||||
return resp, errors.BadRequest(reason.EmailDuplicate)
|
return resp, errors.BadRequest(reason.EmailDuplicate)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue