Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 解决了错误码覆盖的问题 #72

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func UserInfo(ctx *gin.Context) {
user, err := service.UserInfo(ctx)
if err != nil {
controllerLogger.Errorf("user token error: %s", err.Error())
ctx.JSON(http.StatusOK, result.Failed(result.GetUserinfoFail))
//ctx.JSON(http.StatusOK, result.Failed(result.GetUserinfoFail))
ctx.JSON(http.StatusOK, result.Failed(result.HandleErrorWithArgu(err, result.GetUserinfoFail)))
return
}

Expand Down Expand Up @@ -120,7 +121,8 @@ func SendEmail(ctx *gin.Context) {
// 我开始乱写了啊啊啊啊
if usernameErr != nil {
controllerLogger.Errorf("username parse error: %s", usernameErr.Error())
ctx.JSON(http.StatusUnauthorized, result.Failed(result.TicketNotCorrect))
//ctx.JSON(http.StatusUnauthorized, result.Failed(result.TicketNotCorrect))
ctx.JSON(http.StatusUnauthorized, result.Failed(result.HandleErrorWithArgu(usernameErr, result.TicketNotCorrect)))
return
}
// verify if the user email correct
Expand Down Expand Up @@ -190,15 +192,17 @@ func Login(ctx *gin.Context) {
// Get username from ticket
username, err := util.GetUsername(ticket, model.LOGIN_TICKET_SUB)
if err != nil || username == "" {
ctx.JSON(http.StatusOK, result.Failed(result.TicketNotCorrect))
//ctx.JSON(http.StatusOK, result.Failed(result.TicketNotCorrect))
ctx.JSON(http.StatusOK, result.Failed(result.HandleErrorWithArgu(err, result.TicketNotCorrect)))
return
}

uid, err := service.Login(username, password)
if err != nil {
controllerLogger.Errorf("login fail: %s", err.Error())
//ctx.JSON(http.StatusUnauthorized, result.Failed(result.VerifyAccountError))
ctx.AbortWithStatusJSON(http.StatusUnauthorized, result.Failed(result.VerifyPasswordError))
//ctx.AbortWithStatusJSON(http.StatusUnauthorized, result.Failed(result.VerifyPasswordError))
ctx.AbortWithStatusJSON(http.StatusUnauthorized, result.Failed(result.HandleErrorWithArgu(err, result.VerifyPasswordError)))
return
}
if uid == "" {
Expand Down
20 changes: 15 additions & 5 deletions model/result/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ var (
VerifyAccountError = LocalError{ErrCode: 40001, ErrMsg: "验证账户失败"}
VerifyPasswordError = LocalError{ErrCode: 40002, ErrMsg: "验证账户密码失败"}
// this is default error
InternalErr = LocalError{ErrCode: 50000, ErrMsg: "未知错误"}
ClientErr = LocalError{ErrCode: 60001, ErrMsg: "客户端错误"}
AccessTokenErr = LocalError{ErrCode: 60002, ErrMsg: "access_token错误"}
RefreshTokenErr = LocalError{ErrCode: 60003, ErrMsg: "refresh_token错误"}
InternalErr = LocalError{ErrCode: 50000, ErrMsg: "未知错误"}
ClientErr = LocalError{ErrCode: 60001, ErrMsg: "客户端错误"}
AccessTokenErr = LocalError{ErrCode: 60002, ErrMsg: "access_token错误"}
RefreshTokenErr = LocalError{ErrCode: 60003, ErrMsg: "refresh_token错误"}
RegisterPhaseError = LocalError{ErrCode: 70003, ErrMsg: "注册失败 (!!!!hack????)"}
ResetPasswordEror = LocalError{ErrCode: 70004, ErrMsg: "重置密码失败 (!!!!hack????)"}
AlreadySetPasswordErr = LocalError{ErrCode: 70004, ErrMsg: "重复设置密码"}
Expand Down Expand Up @@ -115,11 +115,21 @@ func (e *LocalError) Is(err error) bool {
// use this function to get the errors.
func HandleError(err error) LocalError {
if err, ok := err.(LocalError); ok {
// determine whether the error is exist in errorMap
// determine whether the error is existed in errorMap
if _, ok := errorMap[err.ErrCode]; ok {
return err
}
}
// if not exist, return default error
return InternalErr.Wrap(err)
}
func HandleErrorWithArgu(err error, localError LocalError) LocalError {
if err, ok := err.(LocalError); ok {
// determine whether the error is existed in errorMap
if _, ok := errorMap[err.ErrCode]; ok {
return err
}
}
// if not exist, return default error warped with specified localError
return localError.Wrap(err)
}