agent-hub-runtime 0.6.0

AgentHub desktop and companion CLI runtime for Tauri applications
Documentation
use serde::Serialize;
use std::fmt;

/// Structured error type for Tauri commands.
///
/// Serialized as `{"kind": "Database", "message": "..."}` so the frontend
/// can branch on `kind` while still showing a human-readable `message`.
///
/// `details` carries machine-readable specifics for the few kinds where the
/// caller has to do more than print the message. It is omitted from the wire
/// format when absent, so every existing consumer is unaffected.
#[derive(Debug, Serialize)]
pub struct AppError {
    pub kind: ErrorKind,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<ErrorDetails>,
}

/// The paths a deployment refused to write, because they belong to someone
/// else (#363). Nothing at them was touched. Carried only by
/// `ErrorKind::TargetConflict`, which is what says how to read them — the ways
/// out are documented once, in the `agent-hub-cli` skill, not repeated here on
/// every error.
#[derive(Debug, Serialize)]
pub struct ErrorDetails {
    pub conflicts: Vec<TargetConflictDetail>,
}

#[derive(Debug, Serialize)]
pub struct TargetConflictDetail {
    pub path: String,
    pub reason: String,
    /// The target path already contains a valid skill (has SKILL.md).
    pub is_skill: bool,
}

#[derive(Debug, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorKind {
    Database,
    Io,
    Network,
    Git,
    NotFound,
    InvalidInput,
    Cancelled,
    Internal,
    /// Project content requires explicit consent before a center restore.
    ProjectRestoreRequired,
    /// A write was refused because the target is not ours to replace. Always
    /// carries `ErrorDetails::TargetConflict`.
    TargetConflict,
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl AppError {
    pub fn not_found(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::NotFound,
            message: msg.into(),
            details: None,
        }
    }

    pub fn project_restore_required(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::ProjectRestoreRequired,
            message: msg.into(),
            details: None,
        }
    }

    pub fn invalid_input(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::InvalidInput,
            message: msg.into(),
            details: None,
        }
    }

    #[allow(dead_code)]
    pub fn cancelled(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Cancelled,
            message: msg.into(),
            details: None,
        }
    }

    /// Convert an `anyhow::Error` originating from database operations.
    pub fn db(e: impl fmt::Display) -> Self {
        Self {
            kind: ErrorKind::Database,
            message: e.to_string(),
            details: None,
        }
    }

    /// Convert an `anyhow::Error` originating from git operations.
    pub fn git(e: impl fmt::Display) -> Self {
        Self {
            kind: ErrorKind::Git,
            message: e.to_string(),
            details: None,
        }
    }

    /// Classify a git operation error into cancellation, network, or generic git error.
    pub fn classify_git_error(e: impl fmt::Display) -> Self {
        let message = e.to_string();
        let lower = message.to_ascii_lowercase();
        if lower.contains("cancelled") || lower.contains("canceled") {
            Self {
                kind: ErrorKind::Cancelled,
                message,
                details: None,
            }
        } else if lower.contains("connection refused")
            || lower.contains("could not resolve host")
            || lower.contains("failed to connect")
            || lower.contains("connection timed out")
            || lower.contains("network is unreachable")
        {
            Self {
                kind: ErrorKind::Network,
                message,
                details: None,
            }
        } else {
            Self {
                kind: ErrorKind::Git,
                message,
                details: None,
            }
        }
    }

    /// Convert an `anyhow::Error` originating from network operations.
    pub fn network(e: impl fmt::Display) -> Self {
        Self {
            kind: ErrorKind::Network,
            message: e.to_string(),
            details: None,
        }
    }

    /// Convert an `anyhow::Error` originating from IO operations.
    pub fn io(e: impl fmt::Display) -> Self {
        Self {
            kind: ErrorKind::Io,
            message: e.to_string(),
            details: None,
        }
    }

    /// Preserve the cause chain of a contextual filesystem failure for the UI.
    pub fn io_with_context(error: anyhow::Error) -> Self {
        Self::io(super::log_sanitize::sanitize(&format!("{error:#}")))
    }

    /// A deployment refusal, with the paths that stopped it. Callers that
    /// drive this programmatically (the CLI, and the frontend toast) need the
    /// paths, not a sentence containing them.
    pub fn target_conflict(
        message: impl Into<String>,
        conflicts: Vec<TargetConflictDetail>,
    ) -> Self {
        Self {
            kind: ErrorKind::TargetConflict,
            message: message.into(),
            details: Some(ErrorDetails { conflicts }),
        }
    }

    pub fn internal(e: impl fmt::Display) -> Self {
        Self {
            kind: ErrorKind::Internal,
            message: e.to_string(),
            details: None,
        }
    }
}

impl std::error::Error for AppError {}

impl From<std::io::Error> for AppError {
    fn from(e: std::io::Error) -> Self {
        Self {
            kind: ErrorKind::Io,
            message: e.to_string(),
            details: None,
        }
    }
}

impl From<tokio::task::JoinError> for AppError {
    fn from(e: tokio::task::JoinError) -> Self {
        Self {
            kind: ErrorKind::Internal,
            message: e.to_string(),
            details: None,
        }
    }
}

impl From<tauri::Error> for AppError {
    fn from(e: tauri::Error) -> Self {
        Self {
            kind: ErrorKind::Internal,
            message: e.to_string(),
            details: None,
        }
    }
}

#[cfg(test)]
mod tests;