Skip to main content

agent_tui_common/common/
daemon_error.rs

1//! Daemon error helpers.
2
3use crate::common::error_codes;
4use crate::common::error_codes::ErrorCategory;
5use std::error::Error as StdError;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum DaemonError {
10    #[error("Failed to bind socket ({operation}): {source}")]
11    SocketBind {
12        operation: &'static str,
13        #[source]
14        source: Box<dyn StdError + Send + Sync>,
15    },
16    #[error("Another daemon instance is already running")]
17    AlreadyRunning,
18    #[error("Failed to acquire lock ({operation}): {source}")]
19    LockFailed {
20        operation: &'static str,
21        #[source]
22        source: Box<dyn StdError + Send + Sync>,
23    },
24    #[error("Failed to setup signal handler: {0}")]
25    SignalSetup(String),
26    #[error("Failed to create thread pool: {0}")]
27    ThreadPool(String),
28    #[error("Failed to initialize daemon during {operation}: {source}")]
29    Startup {
30        operation: &'static str,
31        #[source]
32        source: Box<dyn StdError + Send + Sync>,
33    },
34}
35
36impl DaemonError {
37    pub fn code(&self) -> i32 {
38        error_codes::DAEMON_ERROR
39    }
40
41    pub fn category(&self) -> ErrorCategory {
42        ErrorCategory::External
43    }
44
45    pub fn suggestion(&self) -> String {
46        match self {
47            DaemonError::SocketBind { .. } => {
48                "Check that the Unix socket directory is writable. Inspect the active socket path with 'agent-tui env' and remove any stale socket file.".to_string()
49            }
50            DaemonError::AlreadyRunning => {
51                "Another daemon is running. Use 'agent-tui sessions' to connect or kill existing daemon."
52                    .to_string()
53            }
54            DaemonError::LockFailed { .. } => {
55                "Lock file issue. Inspect the active socket path with 'agent-tui env' and remove the matching stale .lock file if needed.".to_string()
56            }
57            DaemonError::SignalSetup(_) => {
58                "Signal handler setup failed. Check system signal configuration.".to_string()
59            }
60            DaemonError::ThreadPool(_) => {
61                "Thread pool creation failed. Check system thread limits (ulimit -u).".to_string()
62            }
63            DaemonError::Startup { .. } => {
64                "Daemon startup could not restore or verify session state. Inspect the session store path from 'agent-tui env' and fix any stale or invalid files before retrying.".to_string()
65            }
66        }
67    }
68
69    pub fn is_retryable(&self) -> bool {
70        matches!(self, DaemonError::LockFailed { .. })
71    }
72}