zilliz 0.1.1

TUI and CLI tool for managing Zilliz Cloud clusters and Milvus operations
Documentation
/// Return a user-friendly hint for a common API error, or None if no hint applies.
pub fn get_error_hint(
    status_code: Option<u16>,
    error_code: i64,
    message: &str,
) -> Option<String> {
    let msg_lower = message.to_lowercase();

    if matches!(status_code, Some(401) | Some(403)) || error_code == 80001 {
        return Some(
            "Check your API key with `zilliz configure` or re-run `zilliz login`.".to_string(),
        );
    }

    if msg_lower.contains("permissiondenied") || msg_lower.contains("permission deny") {
        return Some(
            "This may require a Dedicated cluster plan, or your API key may lack the required permissions.".to_string(),
        );
    }

    if matches!(status_code, Some(429)) || msg_lower.contains("rate limit") {
        return Some(
            "Rate limit reached. Wait a moment and retry, or reduce request frequency.".to_string(),
        );
    }

    if msg_lower.contains("timeout") || msg_lower.contains("timed out") {
        return Some(
            "Request timed out. Check your network connection and try again.".to_string(),
        );
    }

    if msg_lower.contains("connection") || msg_lower.contains("connect") {
        return Some(
            "Connection failed. Check your network and that the endpoint is reachable.".to_string(),
        );
    }

    if matches!(status_code, Some(500..=599)) {
        return Some(
            "Server error. This is usually temporary -- retry in a few seconds.".to_string(),
        );
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_auth_error() {
        let hint = get_error_hint(Some(401), 0, "Unauthorized");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("API key"));
    }

    #[test]
    fn test_error_code_80001() {
        let hint = get_error_hint(None, 80001, "some message");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("API key"));
    }

    #[test]
    fn test_rate_limit() {
        let hint = get_error_hint(Some(429), 0, "Too many requests");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("Rate limit"));
    }

    #[test]
    fn test_server_error() {
        let hint = get_error_hint(Some(502), 0, "Bad Gateway");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("Server error"));
    }

    #[test]
    fn test_no_hint() {
        let hint = get_error_hint(Some(200), 0, "Success");
        assert!(hint.is_none());
    }

    #[test]
    fn test_permission_denied_case_insensitive() {
        let hint = get_error_hint(None, 0, "PermissionDenied: access not allowed");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("Dedicated"));
    }

    #[test]
    fn test_timeout_hint() {
        let hint = get_error_hint(None, 0, "Request Timed Out after 30s");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("timed out"));
    }

    #[test]
    fn test_connection_hint() {
        let hint = get_error_hint(None, 0, "Connection refused");
        assert!(hint.is_some());
        assert!(hint.unwrap().contains("Connection failed"));
    }
}