Skip to content

Commit 635e556

Browse files
committed
refactor: improve error handling for user login state
1 parent ef7fd2c commit 635e556

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

bot.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (b *Bot) Alive() bool {
5252
// fmt.Println(self.NickName)
5353
func (b *Bot) GetCurrentUser() (*Self, error) {
5454
if b.self == nil {
55-
return nil, errors.New("user not login")
55+
return nil, ErrUserNotLogin
5656
}
5757
return b.self, nil
5858
}
@@ -98,15 +98,14 @@ func (b *Bot) PushLogin(storage HotReloadStorage, opts ...BotLoginOption) error
9898

9999
// Logout 用户退出
100100
func (b *Bot) Logout() error {
101-
if b.Alive() {
102-
info := b.Storage.LoginInfo
103-
if err := b.Caller.Logout(b.Context(), info); err != nil {
104-
return err
105-
}
106-
b.ExitWith(ErrUserLogout)
107-
return nil
101+
if !b.Alive() {
102+
return ErrUserNotLogin
108103
}
109-
return errors.New("user not login")
104+
if err := b.Caller.Logout(b.Context(), b.Storage.LoginInfo); err != nil {
105+
return err
106+
}
107+
b.ExitWith(ErrUserLogout)
108+
return nil
110109
}
111110

112111
// loginFromURL 登录逻辑

errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ var (
3838

3939
// ErrUserLogout define user logout error
4040
ErrUserLogout = errors.New("user logout")
41+
42+
// ErrUserNotLogin define user not login
43+
ErrUserNotLogin = errors.New("user not login")
4144
)
4245

4346
// Error impl error interface

0 commit comments

Comments
 (0)