Skip to content

Commit 07573f7

Browse files
committed
增加错误码和健康恢复处理
1 parent b036ae3 commit 07573f7

File tree

5 files changed

+135
-49
lines changed

5 files changed

+135
-49
lines changed

client/candy.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type MessageHandler interface {
2525
// OnError 连接被服务器断开,或其它错误
2626
OnError(msg string)
2727

28+
// OnHealth 连接正常
29+
OnHealth()
30+
2831
// OnUnHealth 连接异常
2932
OnUnHealth(msg string)
3033
}
@@ -74,11 +77,11 @@ func (c *CandyClient) Stop() error {
7477

7578
// Register 用户注册接口
7679
func (c *CandyClient) Register(user, passwd string) (int64, error) {
77-
if err := CheckUserName(user); err != nil {
78-
return -1, err
80+
if code, err := CheckUserName(user); err != nil {
81+
return -1, NewError(code, err.Error())
7982
}
80-
if err := CheckUserPassword(passwd); err != nil {
81-
return -1, err
83+
if code, err := CheckUserPassword(passwd); err != nil {
84+
return -1, NewError(code, err.Error())
8285
}
8386

8487
req := &meta.GateRegisterRequest{User: user, Password: passwd}
@@ -92,12 +95,12 @@ func (c *CandyClient) Register(user, passwd string) (int64, error) {
9295

9396
// Login 用户登陆
9497
func (c *CandyClient) Login(user, passwd string) (int64, error) {
95-
if err := CheckUserName(user); err != nil {
96-
return -1, err
98+
if code, err := CheckUserName(user); err != nil {
99+
return -1, NewError(code, err.Error())
97100
}
98101

99-
if err := CheckUserPassword(passwd); err != nil {
100-
return -1, err
102+
if code, err := CheckUserPassword(passwd); err != nil {
103+
return -1, NewError(code, err.Error())
101104
}
102105

103106
req := &meta.GateUserLoginRequest{User: user, Password: passwd}
@@ -122,12 +125,12 @@ func (c *CandyClient) Logout() error {
122125

123126
// UpdateUserInfo 更新用户信息, 昵称/头像
124127
func (c *CandyClient) UpdateUserInfo(user, nickName string, avatar []byte) (int64, error) {
125-
if err := CheckUserName(user); err != nil {
126-
return -1, err
128+
if code, err := CheckUserName(user); err != nil {
129+
return -1, NewError(code, err.Error())
127130
}
128131

129-
if err := CheckNickName(nickName); err != nil {
130-
return -1, err
132+
if code, err := CheckNickName(nickName); err != nil {
133+
return -1, NewError(code, err.Error())
131134
}
132135

133136
req := &meta.GateUpdateUserInfoRequest{User: user, NickName: nickName, Avatar: avatar}
@@ -141,12 +144,12 @@ func (c *CandyClient) UpdateUserInfo(user, nickName string, avatar []byte) (int6
141144

142145
// UpdateUserPassword 更新用户密码
143146
func (c *CandyClient) UpdateUserPassword(user, passwd string) (int64, error) {
144-
if err := CheckUserName(user); err != nil {
145-
return -1, err
147+
if code, err := CheckUserName(user); err != nil {
148+
return -1, NewError(code, err.Error())
146149
}
147150

148-
if err := CheckUserPassword(passwd); err != nil {
149-
return -1, err
151+
if code, err := CheckUserPassword(passwd); err != nil {
152+
return -1, NewError(code, err.Error())
150153
}
151154

152155
req := &meta.GateUpdateUserPasswordRequest{User: user, Password: passwd}
@@ -175,8 +178,8 @@ func (c *CandyClient) GetUserInfoByName(user string) (string, error) {
175178
}
176179

177180
func (c *CandyClient) getUserInfoByName(user string) (*UserInfo, error) {
178-
if err := CheckUserName(user); err != nil {
179-
return nil, err
181+
if code, err := CheckUserName(user); err != nil {
182+
return nil, NewError(code, err.Error())
180183
}
181184

182185
req := &meta.GateGetUserInfoRequest{Type: 0, UserName: user}

client/candy_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func (c *cmdClient) OnError(msg string) {
3131
fmt.Printf("rpc error:%s\n", msg)
3232
}
3333

34+
// OnHealth 连接正常
35+
func (c *cmdClient) OnHealth() {
36+
fmt.Printf("connection recovery\n")
37+
}
38+
3439
// OnUnHealth 连接异常
3540
func (c *cmdClient) OnUnHealth(msg string) {
3641
fmt.Printf("connection unhealth, msg:%v", msg)

client/client/client.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func register(c *candy.CandyClient, reader *bufio.Reader) {
4040

4141
id, err := c.Register(userName, userPassword)
4242
if err != nil {
43-
log.Errorf("Register error:%v", err)
43+
e := candy.ErrorParse(err.Error())
44+
log.Errorf("Register code:%v error:%v", e.Code, e.Msg)
4445
return
4546
}
4647

@@ -60,7 +61,8 @@ func login(c *candy.CandyClient, reader *bufio.Reader) {
6061

6162
id, err := c.Login(userName, userPassword)
6263
if err != nil {
63-
log.Errorf("Login error:%v", err)
64+
e := candy.ErrorParse(err.Error())
65+
log.Errorf("Login code:%v error:%v", e.Code, e.Msg)
6466
return
6567
}
6668

@@ -72,7 +74,8 @@ func logout(c *candy.CandyClient, reader *bufio.Reader) {
7274
fmt.Println("================注销=======================")
7375
err := c.Logout()
7476
if err != nil {
75-
log.Errorf("Logout error:%v", err)
77+
e := candy.ErrorParse(err.Error())
78+
log.Errorf("Logout code:%v error:%v", e.Code, e.Msg)
7679
return
7780
}
7881

@@ -91,7 +94,8 @@ func updateUserInfo(c *candy.CandyClient, reader *bufio.Reader) {
9194

9295
id, err := c.UpdateUserInfo(userName, nickName, nil)
9396
if err != nil {
94-
log.Errorf("updateUserInfo error:%v", err)
97+
e := candy.ErrorParse(err.Error())
98+
log.Errorf("updateUserInfo code:%v error:%v", e.Code, e.Msg)
9599
return
96100
}
97101

@@ -107,13 +111,15 @@ func getUserInfoByName(c *candy.CandyClient, reader *bufio.Reader) {
107111

108112
data, err := c.GetUserInfoByName(userName)
109113
if err != nil {
110-
log.Errorf("getUserInfo error:%v", err)
114+
e := candy.ErrorParse(err.Error())
115+
log.Errorf("getUserInfo code:%v error:%v", e.Code, e.Msg)
111116
return
112117
}
113118

114119
user, err := candy.DecodeUserInfo([]byte(data))
115120
if err != nil {
116-
log.Errorf("Decode UserInfo error:%v", err)
121+
e := candy.ErrorParse(err.Error())
122+
log.Errorf("Decode UserInfo code:%v error:%v", e.Code, e.Msg)
117123
return
118124
}
119125

@@ -136,13 +142,15 @@ func getUserInfoByID(c *candy.CandyClient, reader *bufio.Reader) {
136142

137143
data, err := c.GetUserInfoByID(id)
138144
if err != nil {
139-
log.Errorf("getUserInfoByID error:%v", err)
145+
e := candy.ErrorParse(err.Error())
146+
log.Errorf("getUserInfoByID code:%v error:%v", e.Code, e.Msg)
140147
return
141148
}
142149

143150
user, err := candy.DecodeUserInfo([]byte(data))
144151
if err != nil {
145-
log.Errorf("Decode UserInfo error:%v", err)
152+
e := candy.ErrorParse(err.Error())
153+
log.Errorf("Decode UserInfo code:%v error:%v", e.Code, e.Msg)
146154
return
147155
}
148156

@@ -159,13 +167,15 @@ func findUser(c *candy.CandyClient, reader *bufio.Reader) {
159167

160168
data, err := c.FindUser(userName)
161169
if err != nil {
162-
log.Errorf("findUser error:%v", err)
170+
e := candy.ErrorParse(err.Error())
171+
log.Errorf("findUser code:%v error:%v", e.Code, e.Msg)
163172
return
164173
}
165174

166175
userList, err := candy.DecodeUserList([]byte(data))
167176
if err != nil {
168-
log.Errorf("Decode UserList error:%v", err)
177+
e := candy.ErrorParse(err.Error())
178+
log.Errorf("Decode UserList code:%v error:%v", e.Code, e.Msg)
169179
return
170180
}
171181

@@ -186,7 +196,8 @@ func addFriend(c *candy.CandyClient, reader *bufio.Reader) {
186196

187197
id, err := strconv.ParseInt(userID, 10, 64)
188198
if err != nil {
189-
log.Errorf("Parse int error:%v", err)
199+
e := candy.ErrorParse(err.Error())
200+
log.Errorf("Parse int code:%v error:%v", e.Code, e.Msg)
190201
return
191202
}
192203

@@ -196,7 +207,8 @@ func addFriend(c *candy.CandyClient, reader *bufio.Reader) {
196207

197208
confirm, err := c.AddFriend(id, true, msg)
198209
if err != nil {
199-
log.Errorf("addFriend error:%v", err)
210+
e := candy.ErrorParse(err.Error())
211+
log.Errorf("addFriend code:%v error:%v", e.Code, e.Msg)
200212
return
201213
}
202214

@@ -212,7 +224,8 @@ func newMessage(c *candy.CandyClient, reader *bufio.Reader) {
212224

213225
from, err := strconv.ParseInt(userID, 10, 64)
214226
if err != nil {
215-
log.Errorf("Parse int error:%v", err)
227+
e := candy.ErrorParse(err.Error())
228+
log.Errorf("Parse int code:%v error:%v", e.Code, e.Msg)
216229
return
217230
}
218231

@@ -222,7 +235,8 @@ func newMessage(c *candy.CandyClient, reader *bufio.Reader) {
222235

223236
user, err := strconv.ParseInt(userID, 10, 64)
224237
if err != nil {
225-
log.Errorf("Parse int error:%v", err)
238+
e := candy.ErrorParse(err.Error())
239+
log.Errorf("Parse int code:%v error:%v", e.Code, e.Msg)
226240
return
227241
}
228242

@@ -232,7 +246,8 @@ func newMessage(c *candy.CandyClient, reader *bufio.Reader) {
232246

233247
err = c.SendMessage(from, 0, user, msg)
234248
if err != nil {
235-
log.Errorf("send message error:%v", err)
249+
e := candy.ErrorParse(err.Error())
250+
log.Errorf("send message code:%v error:%v", e.Code, e.Msg)
236251
return
237252
}
238253

@@ -248,7 +263,8 @@ func createGroup(c *candy.CandyClient, reader *bufio.Reader) {
248263

249264
gid, err := c.CreateGroup(groupName)
250265
if err != nil {
251-
log.Errorf("createGroup error:%v", err)
266+
e := candy.ErrorParse(err.Error())
267+
log.Errorf("createGroup code:%v error:%v", e.Code, e.Msg)
252268
return
253269
}
254270

@@ -261,13 +277,15 @@ func loadGroupList(c *candy.CandyClient, reader *bufio.Reader) {
261277

262278
data, err := c.LoadGroupList()
263279
if err != nil {
264-
log.Errorf("loadGroupList error:%v", err)
280+
e := candy.ErrorParse(err.Error())
281+
log.Errorf("loadGroupList code:%v error:%v", e.Code, e.Msg)
265282
return
266283
}
267284

268285
gList, err := candy.DecodeGroupList([]byte(data))
269286
if err != nil {
270-
log.Errorf("Decode GroupList error:%v", err)
287+
e := candy.ErrorParse(err.Error())
288+
log.Errorf("Decode GroupList code:%v error:%v", e.Code, e.Msg)
271289
return
272290
}
273291

@@ -284,13 +302,15 @@ func loadFriendList(c *candy.CandyClient, reader *bufio.Reader) {
284302

285303
data, err := c.LoadFriendList()
286304
if err != nil {
287-
log.Errorf("loadGroupList error:%v", err)
305+
e := candy.ErrorParse(err.Error())
306+
log.Errorf("loadGroupList code:%v error:%v", e.Code, e.Msg)
288307
return
289308
}
290309

291310
fList, err := candy.DecodeFriendList([]byte(data))
292311
if err != nil {
293-
log.Errorf("Decode FriendList error:%v", err)
312+
e := candy.ErrorParse(err.Error())
313+
log.Errorf("Decode FriendList code:%v error:%v", e.Code, e.Msg)
294314
return
295315
}
296316

@@ -314,7 +334,8 @@ func updateUserPasswd(c *candy.CandyClient, reader *bufio.Reader) {
314334

315335
id, err := c.UpdateUserPassword(user, pwd)
316336
if err != nil {
317-
log.Errorf("UpdateUserPassword error:%v", err)
337+
e := candy.ErrorParse(err.Error())
338+
log.Errorf("UpdateUserPassword code:%v error:%v", e.Code, e.Msg)
318339
return
319340
}
320341
log.Debugf("UpdateUserPassword success, id:%v", id)
@@ -334,6 +355,11 @@ func (c *cmdClient) OnError(msg string) {
334355
fmt.Printf("rpc error:%s\n", msg)
335356
}
336357

358+
// OnHealth 连接恢复
359+
func (c *cmdClient) OnHealth() {
360+
fmt.Printf("connection recovery\n")
361+
}
362+
337363
// OnUnHealth 连接异常
338364
func (c *cmdClient) OnUnHealth(msg string) {
339365
fmt.Printf("connection UnHealth, msg:%v\n", msg)

0 commit comments

Comments
 (0)