diff --git a/api/v1/user.go b/api/v1/user.go index 28e7c87..cb31661 100644 --- a/api/v1/user.go +++ b/api/v1/user.go @@ -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 } @@ -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 @@ -190,7 +192,8 @@ 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 } @@ -198,7 +201,8 @@ func Login(ctx *gin.Context) { 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 == "" { diff --git a/model/result/common.go b/model/result/common.go index 95f8114..f85cd5e 100644 --- a/model/result/common.go +++ b/model/result/common.go @@ -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: "重复设置密码"} @@ -115,7 +115,7 @@ 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 } @@ -123,3 +123,13 @@ func HandleError(err error) LocalError { // 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) +}