Skip to main content

gitflow_gitcode/
error.rs

1//! GitCode CLI 错误解析。
2
3use gitflow_core::{PlatformCliError, platform::Platform};
4
5/// 解析 `gitcode` CLI 的 stderr 输出为统一的 [`PlatformCliError`]。
6///
7/// 优先尝试 JSON 格式解析(`gitcode` 在 API 错误时输出 JSON),
8/// 回退到纯文本模式。
9/// 用户可见消息为中文。
10#[must_use]
11pub fn parse_gitcode_error(stderr: &[u8]) -> PlatformCliError {
12    let text = String::from_utf8_lossy(stderr);
13
14    // 尝试解析 gitcode 的 JSON 错误格式
15    if let Ok(json) = serde_json::from_slice::<serde_json::Value>(stderr)
16        && let Some(msg) = json.get("message").and_then(serde_json::Value::as_str)
17    {
18        let code = json
19            .get("code")
20            .and_then(serde_json::Value::as_str)
21            .map(String::from);
22
23        let user_message: String = match code.as_deref() {
24            Some("UNAUTHORIZED" | "FORBIDDEN") => "认证失败或权限不足".into(),
25            Some("NOT_FOUND") => "资源不存在".into(),
26            _ => format!("GitCode 操作失败:{msg}"),
27        };
28
29        let mut err = PlatformCliError::new(user_message, text.into_owned(), Platform::GitCode);
30        err.hint = Some("运行 `gc auth status` 检查认证状态".into());
31        err.doc_link = Some("https://gitcode.com/gitcode-cli/cli/blob/main/README.md".into());
32        err.code = code;
33        return err;
34    }
35
36    // 回退:纯文本解析
37    let user_message: String = if text.contains("auth") || text.contains("login") {
38        "未登录 GitCode".into()
39    } else {
40        "GitCode CLI 执行失败".into()
41    };
42
43    let mut err = PlatformCliError::new(user_message, text.into_owned(), Platform::GitCode);
44    err.hint = Some("运行 `gc auth login` 完成登录".into());
45    err.doc_link = Some("https://gitcode.com/gitcode-cli/cli/blob/main/README.md".into());
46    err
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_should_parse_gitcode_json_error() {
55        let json = br#"{"message": "Unauthorized", "code": "UNAUTHORIZED"}"#;
56        let err = parse_gitcode_error(json);
57        assert_eq!(err.code.as_deref(), Some("UNAUTHORIZED"));
58        assert_eq!(err.platform, Platform::GitCode);
59        assert!(err.user_message.contains("认证") || err.user_message.contains("权限"));
60    }
61
62    #[test]
63    fn test_should_parse_gitcode_plain_text_error() {
64        let stderr = b"Error: authentication required";
65        let err = parse_gitcode_error(stderr);
66        assert!(err.hint.is_some());
67        assert_eq!(err.platform, Platform::GitCode);
68    }
69
70    #[test]
71    fn test_should_not_leak_raw_stderr() {
72        let stderr = b"gitcode internal panic trace";
73        let err = parse_gitcode_error(stderr);
74        assert!(!err.to_string().contains("internal panic"));
75    }
76
77    #[test]
78    fn test_should_handle_empty_stderr() {
79        let err = parse_gitcode_error(b"");
80        assert!(!err.user_message.is_empty());
81        assert!(err.hint.is_some());
82    }
83
84    #[test]
85    fn test_should_parse_gitcode_not_found_error() {
86        let json = br#"{"message": "Resource not found", "code": "NOT_FOUND"}"#;
87        let err = parse_gitcode_error(json);
88        assert_eq!(err.code.as_deref(), Some("NOT_FOUND"));
89        assert!(err.user_message.contains("资源不存在"));
90    }
91
92    #[test]
93    fn test_should_parse_gitcode_forbidden_error() {
94        let json = br#"{"message": "Forbidden", "code": "FORBIDDEN"}"#;
95        let err = parse_gitcode_error(json);
96        assert_eq!(err.code.as_deref(), Some("FORBIDDEN"));
97        assert!(err.user_message.contains("认证") || err.user_message.contains("权限"));
98    }
99
100    #[test]
101    fn test_should_parse_gitcode_plain_text_login_error() {
102        let stderr = b"Error: login required";
103        let err = parse_gitcode_error(stderr);
104        assert!(err.user_message.contains("登录"));
105        assert_eq!(err.platform, Platform::GitCode);
106    }
107
108    #[test]
109    fn test_should_parse_gitcode_json_error_without_code() {
110        let json = br#"{"message": "Internal server error"}"#;
111        let err = parse_gitcode_error(json);
112        assert!(err.code.is_none());
113        assert!(err.user_message.contains("Internal server error"));
114        assert_eq!(err.platform, Platform::GitCode);
115    }
116}