From 635e5561f3196cb6740f3ffcd36006fa84b61019 Mon Sep 17 00:00:00 2001 From: eatmoreapple Date: Thu, 2 Jan 2025 11:01:16 +0800 Subject: [PATCH] refactor: improve error handling for user login state --- bot.go | 17 ++++++++--------- errors.go | 3 +++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bot.go b/bot.go index 0605a5b..817dc3e 100644 --- a/bot.go +++ b/bot.go @@ -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 } @@ -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 登录逻辑 diff --git a/errors.go b/errors.go index 6a7f99b..1b17513 100644 --- a/errors.go +++ b/errors.go @@ -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