Skip to content

Commit

Permalink
Fix: 解决了错误码覆盖的问题 (#72)
Browse files Browse the repository at this point in the history
但是整体 的错误处理还是有些混乱,感觉可以试着规范下
比如只定义有意义(用户能看懂)的错误码,没意义的打个自定义的log就行?
比如service层尽量不定义localError,而是打log,然后controlller层调用的时候只需要返回一个抽象层次更高的错误就行
但如果service层有localError的话就用handleError处理下
  • Loading branch information
maplestarplayl authored Dec 12, 2023
1 parent 88703cf commit dc51a61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
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)
}

0 comments on commit dc51a61

Please sign in to comment.