作者 唐旭辉

新增错误处理

@@ -12,7 +12,7 @@ type UserController struct { @@ -12,7 +12,7 @@ type UserController struct {
12 // 127.0.0.1:8080/user/login/* 12 // 127.0.0.1:8080/user/login/*
13 //127.0.0.1:8080/user/logout/* 13 //127.0.0.1:8080/user/logout/*
14 14
15 -func(this *UserController)URLMapping(){ 15 +func (this *UserController) URLMapping() {
16 //this.Mapping("/v1/user/login",this.Login) 16 //this.Mapping("/v1/user/login",this.Login)
17 //this.Mapping("/v1/user/select",this.SelectUser) 17 //this.Mapping("/v1/user/select",this.SelectUser)
18 } 18 }
@@ -22,18 +22,18 @@ func(this *UserController)URLMapping(){ @@ -22,18 +22,18 @@ func(this *UserController)URLMapping(){
22 // @Description 登录 22 // @Description 登录
23 // @Param body body string true "body" 23 // @Param body body string true "body"
24 // @router /v1/user/login [post] 24 // @router /v1/user/login [post]
25 -func(this *UserController)Login(){ 25 +func (this *UserController) Login() {
26 var msg *mybeego.Message 26 var msg *mybeego.Message
27 - defer func(){ 27 + defer func() {
28 this.Resp(msg) 28 this.Resp(msg)
29 }() 29 }()
30 - msg = mybeego.NewErrMessage(0,"login success") 30 + msg = mybeego.NewErrMessage(0, "login success")
31 } 31 }
32 32
33 -func(this *UserController)Logout(){ 33 +func (this *UserController) Logout() {
34 var msg *mybeego.Message 34 var msg *mybeego.Message
35 - defer func(){ 35 + defer func() {
36 this.Resp(msg) 36 this.Resp(msg)
37 }() 37 }()
38 - msg = mybeego.NewErrMessage(0,"logout success")  
39 -}  
  38 + msg = mybeego.NewErrMessage(0, "logout success")
  39 +}
@@ -71,6 +71,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ @@ -71,6 +71,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
71 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= 71 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
72 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 72 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
73 github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= 73 github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
  74 +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
74 github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 75 github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
75 github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= 76 github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
76 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= 77 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
  1 +package protocol
  2 +
  3 +import (
  4 + "encoding/json"
  5 +)
  6 +
  7 +type CustomErrParse interface {
  8 + ParseToMessage() interface{}
  9 +}
  10 +
  11 +//ErrorCode 统一错误编码结构
  12 +type ErrorCode struct {
  13 + Errno int `json:"code"`
  14 + Errmsg string `json:"msg"`
  15 +}
  16 +
  17 +//ResponseMessage 统一返回消息结构体
  18 +type ResponseMessage struct {
  19 + ErrorCode
  20 + Data interface{} `json:"data"`
  21 +}
  22 +
  23 +func NewMesage(code int) *ResponseMessage {
  24 + return &ResponseMessage{
  25 + ErrorCode: errmessge.Search(code),
  26 + Data: nil,
  27 + }
  28 +}
  29 +
  30 +//ErrWithMessage 自定义错误结构
  31 +type ErrWithMessage struct {
  32 + Err error
  33 + ErrorCode
  34 +}
  35 +
  36 +//NewErrWithMessage 构建错误返回
  37 +//code:用于匹配统一消息错误编码 eRR:填充嵌套错误
  38 +func NewErrWithMessage(code int, eRR ...error) *ErrWithMessage {
  39 + r := &ErrWithMessage{
  40 + ErrorCode: errmessge.Search(code),
  41 + }
  42 + if len(eRR) > 0 {
  43 + r.Err = eRR[0]
  44 + }
  45 + return r
  46 +}
  47 +
  48 +//Error 实现接口error 中的方法
  49 +//将ErrorCode转为json数据,建议用于日志记录
  50 +func (e ErrWithMessage) Error() string {
  51 + bt, _ := json.Marshal(e.ErrorCode)
  52 + return string(bt)
  53 +}
  54 +
  55 +//Unwrap 接口实现
  56 +func (e ErrWithMessage) Unwrap() error {
  57 + return e.Err
  58 +}
  59 +
  60 +//ParseToMessage 实现CustomErrParse的接口
  61 +func (e ErrWithMessage) ParseToMessage() interface{} {
  62 + return &ResponseMessage{
  63 + ErrorCode: e.ErrorCode,
  64 + Data: nil,
  65 + }
  66 +}
1 package protocol 1 package protocol
2 2
3 -import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 3 +//ErrorMap 统一消息错误编码
  4 +type ErrorMap map[int]string
  5 +
  6 +//Search 搜索错误描述
  7 +func (m ErrorMap) Search(code int) ErrorCode {
  8 + if v, ok := m[code]; ok {
  9 + return ErrorCode{
  10 + Errno: code,
  11 + Errmsg: v,
  12 + }
  13 + }
  14 + return ErrorCode{}
  15 +}
  16 +
  17 +var errmessge ErrorMap = map[int]string{
  18 + 1: "系统异常",
  19 + 101: "clientId或clientSecret无效",
  20 + 113: "签名验证失败",
  21 + 1009: "验证码已超时,登录失败",
  22 + 1011: "短信验证码次数超过限制,请稍后重试",
  23 + 1012: "验证码错误",
  24 + 2001: "请输入正确的手机号码",
  25 + 2002: "后台未配置账号信息,请联系管理员配置",
  26 + 2009: "上传的文件流为空",
  27 + 2020: "帐号不存在,请联系管理员",
  28 + 2021: "登录失败,手机号或密码错误",
  29 + 2025: "短信验证码验证失败",
  30 + 2026: "两次输入的密码不一致",
  31 + 4139: "authCode无效或过期",
  32 + 4140: "refreshToken过期,需要重新登录授权",
  33 + 4141: "accessToken过期或无效,需要进行重新获取令牌",
  34 + 4142: "Uuid已存在,请求失败",
  35 +}
4 36
5 func InitMessageCode() { 37 func InitMessageCode() {
6 - messages := []struct {  
7 - Code int  
8 - Msg string  
9 - }{  
10 - {101, "clientId或clientSecret无效"},  
11 - {113, "签名验证失败"},  
12 - {1009, "验证码已超时,登录失败"},  
13 - {1011, "短信验证码次数超过限制,请稍后重试"},  
14 - {1012, "验证码错误"}, 38 + // messages := []struct {
  39 + // Code int
  40 + // Msg string
  41 + // }{
  42 + // {101, "clientId或clientSecret无效"},
  43 + // {113, "签名验证失败"},
  44 + // {1009, "验证码已超时,登录失败"},
  45 + // {1011, "短信验证码次数超过限制,请稍后重试"},
  46 + // {1012, "验证码错误"},
15 47
16 - {2001, "请输入正确的手机号码"},  
17 - {2002, "后台未配置账号信息,请联系管理员配置"},  
18 - {2009, "上传的文件流为空"},  
19 - {2020, "帐号不存在,请联系管理员"},  
20 - {2021, "登录失败,手机号或密码错误"},  
21 - {2025, "短信验证码验证失败"},  
22 - {2026, "两次输入的密码不一致"}, 48 + // {2001, "请输入正确的手机号码"},
  49 + // {2002, "后台未配置账号信息,请联系管理员配置"},
  50 + // {2009, "上传的文件流为空"},
  51 + // {2020, "帐号不存在,请联系管理员"},
  52 + // {2021, "登录失败,手机号或密码错误"},
  53 + // {2025, "短信验证码验证失败"},
  54 + // {2026, "两次输入的密码不一致"},
  55 +
  56 + // {4139, "authCode无效或过期"},
  57 + // {4140, "refreshToken过期,需要重新登录授权"},
  58 + // {4141, "accessToken过期或无效,需要进行重新获取令牌"},
  59 + // {4142, "Uuid已存在,请求失败"},
  60 + // }
  61 + // for i := range messages {
  62 + // mybeego.SetMessage(messages[i].Code, messages[i].Msg)
  63 + // }
  64 + ////
23 65
24 - {4139, "authCode无效或过期"},  
25 - {4140, "refreshToken过期,需要重新登录授权"},  
26 - {4141, "accessToken过期或无效,需要进行重新获取令牌"},  
27 - {4142, "Uuid已存在,请求失败"},  
28 - }  
29 - for i := range messages {  
30 - mybeego.SetMessage(messages[i].Code, messages[i].Msg)  
31 - }  
32 } 66 }
@@ -92,18 +92,18 @@ Success: @@ -92,18 +92,18 @@ Success:
92 if userInfo.Auth == "" { 92 if userInfo.Auth == "" {
93 userInfo.Auth = uid.NewV1().StringNoDash() 93 userInfo.Auth = uid.NewV1().StringNoDash()
94 } 94 }
95 - if checkImResponse,err=CheckIm(&protocol.CheckImRequest{  
96 - Uid:fmt.Sprintf("%v",userInfo.Id),  
97 - Uname:userInfo.Uname,  
98 - Icon:userInfo.Icon,  
99 - IsCreated:userInfo.ImToken!="",  
100 - });err!=nil{ 95 + if checkImResponse, err = CheckIm(&protocol.CheckImRequest{
  96 + Uid: fmt.Sprintf("%v", userInfo.Id),
  97 + Uname: userInfo.Uname,
  98 + Icon: userInfo.Icon,
  99 + IsCreated: userInfo.ImToken != "",
  100 + }); err != nil {
101 return 101 return
102 } 102 }
103 - if checkImResponse!=nil && checkImResponse.ImToken!=""{ 103 + if checkImResponse != nil && checkImResponse.ImToken != "" {
104 userInfo.ImToken = checkImResponse.ImToken 104 userInfo.ImToken = checkImResponse.ImToken
105 } 105 }
106 - if userInfo.CsAccount==0{ 106 + if userInfo.CsAccount == 0 {
107 userInfo.CsAccount = imGetRandomCSAccount() 107 userInfo.CsAccount = imGetRandomCSAccount()
108 } 108 }
109 userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire) 109 userInfo.AuthExp = time.Now().Add(time.Second * protocol.TokenExpire)
@@ -132,6 +132,9 @@ func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.Logi @@ -132,6 +132,9 @@ func (s *AuthService) Login1(request *protocol.LoginRequest) (rsp *protocol.Logi
132 if err != nil { 132 if err != nil {
133 return nil, err 133 return nil, err
134 } 134 }
  135 + rsp = &protocol.LoginResponse{
  136 + AuthCode: useridentity.Auth,
  137 + }
135 if len(useridentity.Auth) == 0 { 138 if len(useridentity.Auth) == 0 {
136 useridentity.Auth = uid.NewV1().StringNoDash() 139 useridentity.Auth = uid.NewV1().StringNoDash()
137 } 140 }
@@ -489,20 +492,19 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR @@ -489,20 +492,19 @@ func imUserRefreshToken(request *protocol.CheckImRequest, rsp *protocol.CheckImR
489 } 492 }
490 493
491 // 获取客服id 494 // 获取客服id
492 -func imGetRandomCSAccount()( acid int64){  
493 - kefus,err :=models.GetUsersKefu()  
494 - if err!=nil{ 495 +func imGetRandomCSAccount() (acid int64) {
  496 + kefus, err := models.GetUsersKefu()
  497 + if err != nil {
495 log.Error(err) 498 log.Error(err)
496 return 499 return
497 } 500 }
498 - if len(kefus)<=0{ 501 + if len(kefus) <= 0 {
499 return 502 return
500 } 503 }
501 - index :=rand.Intn(len(kefus))  
502 - acid,err = strconv.ParseInt(kefus[index].Accid,10,64)  
503 - if err!=nil{ 504 + index := rand.Intn(len(kefus))
  505 + acid, err = strconv.ParseInt(kefus[index].Accid, 10, 64)
  506 + if err != nil {
504 log.Error(err) 507 log.Error(err)
505 } 508 }
506 return acid 509 return acid
507 } 510 }
508 -