作者 唐旭辉

新增错误处理

... ... @@ -12,7 +12,7 @@ type UserController struct {
// 127.0.0.1:8080/user/login/*
//127.0.0.1:8080/user/logout/*
func(this *UserController)URLMapping(){
func (this *UserController) URLMapping() {
//this.Mapping("/v1/user/login",this.Login)
//this.Mapping("/v1/user/select",this.SelectUser)
}
... ... @@ -22,18 +22,18 @@ func(this *UserController)URLMapping(){
// @Description 登录
// @Param body body string true "body"
// @router /v1/user/login [post]
func(this *UserController)Login(){
func (this *UserController) Login() {
var msg *mybeego.Message
defer func(){
defer func() {
this.Resp(msg)
}()
msg = mybeego.NewErrMessage(0,"login success")
msg = mybeego.NewErrMessage(0, "login success")
}
func(this *UserController)Logout(){
func (this *UserController) Logout() {
var msg *mybeego.Message
defer func(){
defer func() {
this.Resp(msg)
}()
msg = mybeego.NewErrMessage(0,"logout success")
}
\ No newline at end of file
msg = mybeego.NewErrMessage(0, "logout success")
}
... ...
... ... @@ -71,6 +71,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
... ...
package protocol
import (
"encoding/json"
)
type CustomErrParse interface {
ParseToMessage() interface{}
}
//ErrorCode 统一错误编码结构
type ErrorCode struct {
Errno int `json:"code"`
Errmsg string `json:"msg"`
}
//ResponseMessage 统一返回消息结构体
type ResponseMessage struct {
ErrorCode
Data interface{} `json:"data"`
}
func NewMesage(code int) *ResponseMessage {
return &ResponseMessage{
ErrorCode: errmessge.Search(code),
Data: nil,
}
}
//ErrWithMessage 自定义错误结构
type ErrWithMessage struct {
Err error
ErrorCode
}
//NewErrWithMessage 构建错误返回
//code:用于匹配统一消息错误编码 eRR:填充嵌套错误
func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage {
r := &ErrWithMessage{
ErrorCode: errmessge.Search(code),
}
if len(eRR) > 0 {
r.Err = eRR[0]
}
return r
}
//Error 实现接口error 中的方法
//将ErrorCode转为json数据,建议用于日志记录
func (e ErrWithMessage) Error() string {
bt, _ := json.Marshal(e.ErrorCode)
return string(bt)
}
//Unwrap 接口实现
func (e ErrWithMessage) Unwrap() error {
return e.Err
}
//ParseToMessage 实现CustomErrParse的接口
func (e ErrWithMessage) ParseToMessage() interface{} {
return &ResponseMessage{
ErrorCode: e.ErrorCode,
Data: nil,
}
}
... ...
package protocol
import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
//ErrorMap 统一消息错误编码
type ErrorMap map[int]string
//Search 搜索错误描述
func (m ErrorMap) Search(code int) ErrorCode {
if v, ok := m[code]; ok {
return ErrorCode{
Errno: code,
Errmsg: v,
}
}
return ErrorCode{}
}
var errmessge ErrorMap = map[int]string{
1: "系统异常",
101: "clientId或clientSecret无效",
113: "签名验证失败",
1009: "验证码已超时,登录失败",
1011: "短信验证码次数超过限制,请稍后重试",
1012: "验证码错误",
2001: "请输入正确的手机号码",
2002: "后台未配置账号信息,请联系管理员配置",
2009: "上传的文件流为空",
2020: "帐号不存在,请联系管理员",
2021: "登录失败,手机号或密码错误",
2025: "短信验证码验证失败",
2026: "两次输入的密码不一致",
4139: "authCode无效或过期",
4140: "refreshToken过期,需要重新登录授权",
4141: "accessToken过期或无效,需要进行重新获取令牌",
4142: "Uuid已存在,请求失败",
}
func InitMessageCode() {
messages := []struct {
Code int
Msg string
}{
{101, "clientId或clientSecret无效"},
{113, "签名验证失败"},
{1009, "验证码已超时,登录失败"},
{1011, "短信验证码次数超过限制,请稍后重试"},
{1012, "验证码错误"},
// messages := []struct {
// Code int
// Msg string
// }{
// {101, "clientId或clientSecret无效"},
// {113, "签名验证失败"},
// {1009, "验证码已超时,登录失败"},
// {1011, "短信验证码次数超过限制,请稍后重试"},
// {1012, "验证码错误"},
{2001, "请输入正确的手机号码"},
{2002, "后台未配置账号信息,请联系管理员配置"},
{2009, "上传的文件流为空"},
{2020, "帐号不存在,请联系管理员"},
{2021, "登录失败,手机号或密码错误"},
{2025, "短信验证码验证失败"},
{2026, "两次输入的密码不一致"},
// {2001, "请输入正确的手机号码"},
// {2002, "后台未配置账号信息,请联系管理员配置"},
// {2009, "上传的文件流为空"},
// {2020, "帐号不存在,请联系管理员"},
// {2021, "登录失败,手机号或密码错误"},
// {2025, "短信验证码验证失败"},
// {2026, "两次输入的密码不一致"},
// {4139, "authCode无效或过期"},
// {4140, "refreshToken过期,需要重新登录授权"},
// {4141, "accessToken过期或无效,需要进行重新获取令牌"},
// {4142, "Uuid已存在,请求失败"},
// }
// for i := range messages {
// mybeego.SetMessage(messages[i].Code, messages[i].Msg)
// }
////
{4139, "authCode无效或过期"},
{4140, "refreshToken过期,需要重新登录授权"},
{4141, "accessToken过期或无效,需要进行重新获取令牌"},
{4142, "Uuid已存在,请求失败"},
}
for i := range messages {
mybeego.SetMessage(messages[i].Code, messages[i].Msg)
}
}
... ...
... ... @@ -92,18 +92,18 @@ Success:
if userInfo.Auth == "" {
userInfo.Auth = uid.NewV1().StringNoDash()
}
if checkImResponse,err=CheckIm(&protocol.CheckImRequest{
Uid:fmt.Sprintf("%v",userInfo.Id),
Uname:userInfo.Uname,
Icon:userInfo.Icon,
IsCreated:userInfo.ImToken!="",
});err!=nil{
if checkImResponse, err = CheckIm(&protocol.CheckImRequest{
Uid: fmt.Sprintf("%v", userInfo.Id),
Uname: userInfo.Uname,
Icon: userInfo.Icon,
IsCreated: userInfo.ImToken != "",
}); err != nil {
return
}
if checkImResponse!=nil && checkImResponse.ImToken!=""{
if checkImResponse != nil && checkImResponse.ImToken != "" {
userInfo.ImToken = checkImResponse.ImToken
}
if userInfo.CsAccount==0{
if userInfo.CsAccount == 0 {
userInfo.CsAccount = imGetRandomCSAccount()
}
userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
... ... @@ -132,6 +132,9 @@ func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.Logi
if err != nil {
return nil, err
}
rsp = &protocol.LoginResponse{
AuthCode: useridentity.Auth,
}
if len(useridentity.Auth) == 0 {
useridentity.Auth = uid.NewV1().StringNoDash()
}
... ... @@ -489,20 +492,19 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR
}
// 获取客服id
func imGetRandomCSAccount()( acid int64){
kefus,err :=models.GetUsersKefu()
if err!=nil{
func imGetRandomCSAccount() (acid int64) {
kefus, err := models.GetUsersKefu()
if err != nil {
log.Error(err)
return
}
if len(kefus)<=0{
if len(kefus) <= 0 {
return
}
index :=rand.Intn(len(kefus))
acid,err = strconv.ParseInt(kefus[index].Accid,10,64)
if err!=nil{
index := rand.Intn(len(kefus))
acid, err = strconv.ParseInt(kefus[index].Accid, 10, 64)
if err != nil {
log.Error(err)
}
return acid
}
... ...