use std::fmt;
use crate::platform::Platform;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PlatformCliError {
pub user_message: String,
pub raw_stderr: String,
pub hint: Option<String>,
pub doc_link: Option<String>,
pub code: Option<String>,
pub platform: Platform,
}
impl PlatformCliError {
#[must_use]
pub fn new(
user_message: impl Into<String>,
raw_stderr: impl Into<String>,
platform: Platform,
) -> Self {
Self {
user_message: user_message.into(),
raw_stderr: raw_stderr.into(),
hint: None,
doc_link: None,
code: None,
platform,
}
}
}
impl fmt::Display for PlatformCliError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.user_message)?;
if let Some(ref hint) = self.hint {
write!(f, "\n\n🔧 修复建议:{hint}")?;
}
if let Some(ref link) = self.doc_link {
write!(f, "\n📖 文档:{link}")?;
}
Ok(())
}
}
impl std::error::Error for PlatformCliError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_display_user_message_only_when_no_hint_no_link() {
let err = PlatformCliError {
user_message: "资源不存在".into(),
raw_stderr: "gh: NOT_FOUND".into(),
hint: None,
doc_link: None,
code: Some("NOT_FOUND".into()),
platform: Platform::GitHub,
};
assert_eq!(err.to_string(), "资源不存在");
}
#[test]
fn test_should_display_with_hint_and_doc_link() {
let err = PlatformCliError {
user_message: "认证失败".into(),
raw_stderr: "raw error".into(),
hint: Some("运行 `gh auth login` 重新认证".into()),
doc_link: Some("https://cli.github.com/manual/".into()),
code: None,
platform: Platform::GitHub,
};
let display = err.to_string();
assert!(display.contains("认证失败"));
assert!(display.contains("🔧 修复建议:运行 `gh auth login` 重新认证"));
assert!(display.contains("📖 文档:https://cli.github.com/manual/"));
assert!(!display.contains("raw error"));
}
#[test]
fn test_should_include_raw_stderr_in_debug() {
let err = PlatformCliError {
user_message: "错误".into(),
raw_stderr: "secret debug info".into(),
hint: None,
doc_link: None,
code: None,
platform: Platform::GitLab,
};
let debug = format!("{err:?}");
assert!(debug.contains("secret debug info"));
}
#[test]
fn test_should_create_platform_cli_error_with_new() {
let err = PlatformCliError::new("错误消息", "raw stderr", Platform::GitHub);
assert_eq!(err.user_message, "错误消息");
assert_eq!(err.raw_stderr, "raw stderr");
assert_eq!(err.platform, Platform::GitHub);
assert!(err.hint.is_none());
assert!(err.doc_link.is_none());
assert!(err.code.is_none());
}
#[test]
fn test_should_handle_empty_strings_in_platform_cli_error() {
let err = PlatformCliError::new("", "", Platform::GitLab);
assert_eq!(err.user_message, "");
assert_eq!(err.raw_stderr, "");
assert_eq!(err.to_string(), "");
}
#[test]
fn test_should_set_optional_fields_via_direct_assignment() {
let mut err = PlatformCliError::new("错误", "stderr", Platform::GitCode);
err.hint = Some("尝试重新运行".into());
err.doc_link = Some("https://example.com".into());
err.code = Some("ERR_001".into());
assert_eq!(err.hint.as_deref(), Some("尝试重新运行"));
assert_eq!(err.doc_link.as_deref(), Some("https://example.com"));
assert_eq!(err.code.as_deref(), Some("ERR_001"));
}
use rstest::rstest;
#[rstest]
#[case(Platform::GitHub)]
#[case(Platform::GitLab)]
#[case(Platform::GitCode)]
fn test_should_create_error_for_all_platforms(#[case] platform: Platform) {
let err = PlatformCliError::new("测试", "stderr", platform);
assert_eq!(err.platform, platform);
}
}