Files
huangguo_server/common/imclient/errors.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

42 lines
908 B
Go

package imclient
import (
"fmt"
"net/http"
"strings"
)
const sessionExpiredCode = 401
type APIError struct {
StatusCode int
Code int
Message string
Body []byte
}
func (e *APIError) Error() string {
if e == nil {
return ""
}
if e.Code != 0 {
return fmt.Sprintf("im api error: status=%d code=%d message=%s", e.StatusCode, e.Code, e.Message)
}
return fmt.Sprintf("im api error: status=%d message=%s", e.StatusCode, e.Message)
}
func IsSessionExpired(err error) bool {
apiErr, ok := err.(*APIError)
if !ok || apiErr == nil {
return false
}
if apiErr.Code == sessionExpiredCode || apiErr.StatusCode == http.StatusUnauthorized {
return true
}
msg := strings.ToLower(apiErr.Message)
return strings.Contains(msg, "session has expired") ||
strings.Contains(msg, "log in again") ||
strings.Contains(msg, "token expired") ||
strings.Contains(msg, "token invalid")
}