@@ -0,0 +1,41 @@
|
||||
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")
|
||||
}
|
||||
Reference in New Issue
Block a user