Skip to content

Commit

Permalink
refactor: improve error handling for user login state
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmoreapple committed Jan 2, 2025
1 parent ef7fd2c commit 635e556
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
17 changes: 8 additions & 9 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (b *Bot) Alive() bool {
// fmt.Println(self.NickName)
func (b *Bot) GetCurrentUser() (*Self, error) {
if b.self == nil {
return nil, errors.New("user not login")
return nil, ErrUserNotLogin
}
return b.self, nil
}
Expand Down Expand Up @@ -98,15 +98,14 @@ func (b *Bot) PushLogin(storage HotReloadStorage, opts ...BotLoginOption) error

// Logout 用户退出
func (b *Bot) Logout() error {
if b.Alive() {
info := b.Storage.LoginInfo
if err := b.Caller.Logout(b.Context(), info); err != nil {
return err
}
b.ExitWith(ErrUserLogout)
return nil
if !b.Alive() {
return ErrUserNotLogin
}
return errors.New("user not login")
if err := b.Caller.Logout(b.Context(), b.Storage.LoginInfo); err != nil {
return err
}
b.ExitWith(ErrUserLogout)
return nil
}

// loginFromURL 登录逻辑
Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var (

// ErrUserLogout define user logout error
ErrUserLogout = errors.New("user logout")

// ErrUserNotLogin define user not login
ErrUserNotLogin = errors.New("user not login")
)

// Error impl error interface
Expand Down

0 comments on commit 635e556

Please sign in to comment.