package dingtalkdiscovery import ( "encoding/json" "errors" "fmt" "strings" ) var ErrCredentials = errors.New("DWS authorization required") var ErrPermission = errors.New("DWS directory permission denied") // failureCause inspects only structured CLI errors; no upstream text is returned. func failureCause(raw []byte, cause error) error { var v struct { Error struct { Type string `json:"type"` Code any `json:"code"` Message string `json:"message"` } `json:"error"` } if json.Unmarshal(raw, &v) != nil { return ErrUnavailable } text := strings.ToLower(v.Error.Type + " " + fmt.Sprint(v.Error.Code) + " " + v.Error.Message) for _, key := range []string{"permission", "forbidden", "scope", "access denied", "权限", "403"} { if strings.Contains(text, key) { return ErrPermission } } for _, key := range []string{"unauth", "not_logged_in", "not logged", "login_required", "login required", "authentication", "auth_required", "credential", "token_expired", "invalid_token", "token expired", "invalid_grant", "未登录", "授权已过期", "授权失效"} { if strings.Contains(text, key) { return ErrCredentials } } if errors.Is(cause, ErrCredentials) || errors.Is(cause, ErrPermission) { return cause } return ErrUnavailable }