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"));
}
}