git_worktree_cli/
error.rs

1//! Error types for the git worktree CLI
2//!
3//! This module defines the centralized error handling for the application,
4//! providing context-rich error messages for better debugging and user experience.
5
6use thiserror::Error;
7
8/// The main error type for the git worktree CLI
9#[derive(Error, Debug)]
10pub enum Error {
11    /// IO errors from file system operations
12    #[error("IO error: {0}")]
13    Io(#[from] std::io::Error),
14
15    /// Git command execution errors
16    #[error("Git command failed: {0}")]
17    Git(String),
18
19    /// Configuration parsing or validation errors
20    #[error("Configuration error: {0}")]
21    Config(String),
22
23    /// API provider errors (GitHub, Bitbucket, etc.)
24    #[error("API provider error: {0}")]
25    Provider(String),
26
27    /// Project root or git directory not found
28    #[error("Project root not found")]
29    ProjectRootNotFound,
30
31    /// Git directory not found
32    #[error("Git directory not found in project")]
33    GitDirectoryNotFound,
34
35    /// Branch operation errors
36    #[error("Branch operation failed: {0}")]
37    Branch(String),
38
39    /// Hook execution errors
40    #[error("Hook execution failed: {0}")]
41    Hook(String),
42
43    /// Authentication errors
44    #[error("Authentication error: {0}")]
45    Auth(String),
46
47    /// Network/HTTP request errors
48    #[error("Network error: {0}")]
49    Network(String),
50
51    /// JSON parsing errors
52    #[error("JSON parsing error: {0}")]
53    Json(String),
54
55    /// Regex compilation errors
56    #[error("Regex error: {0}")]
57    Regex(#[from] regex::Error),
58
59    /// Generic errors with context
60    #[error("{0}")]
61    Other(String),
62}
63
64/// Type alias for Results with our Error type
65pub type Result<T> = std::result::Result<T, Error>;
66
67// Convenience functions for creating errors
68impl Error {
69    /// Create a generic error with a message
70    pub fn msg<S: Into<String>>(msg: S) -> Self {
71        Error::Other(msg.into())
72    }
73
74    /// Create a git error
75    pub fn git<S: Into<String>>(msg: S) -> Self {
76        Error::Git(msg.into())
77    }
78
79    /// Create a configuration error
80    pub fn config<S: Into<String>>(msg: S) -> Self {
81        Error::Config(msg.into())
82    }
83
84    /// Create a branch error
85    pub fn branch<S: Into<String>>(msg: S) -> Self {
86        Error::Branch(msg.into())
87    }
88
89    /// Create a hook error
90    pub fn hook<S: Into<String>>(msg: S) -> Self {
91        Error::Hook(msg.into())
92    }
93
94    /// Create an auth error
95    pub fn auth<S: Into<String>>(msg: S) -> Self {
96        Error::Auth(msg.into())
97    }
98
99    /// Create a provider error
100    pub fn provider<S: Into<String>>(msg: S) -> Self {
101        Error::Provider(msg.into())
102    }
103
104    /// Create a network error
105    pub fn network<S: Into<String>>(msg: S) -> Self {
106        Error::Network(msg.into())
107    }
108}
109
110// Helper implementations for common conversions
111impl From<json5::Error> for Error {
112    fn from(err: json5::Error) -> Self {
113        Error::Config(err.to_string())
114    }
115}
116
117impl From<serde_json::Error> for Error {
118    fn from(err: serde_json::Error) -> Self {
119        Error::Json(err.to_string())
120    }
121}
122
123impl From<keyring::Error> for Error {
124    fn from(err: keyring::Error) -> Self {
125        Error::Auth(err.to_string())
126    }
127}
128
129impl From<reqwest::Error> for Error {
130    fn from(err: reqwest::Error) -> Self {
131        Error::Network(err.to_string())
132    }
133}
134
135impl From<std::env::VarError> for Error {
136    fn from(err: std::env::VarError) -> Self {
137        Error::Other(err.to_string())
138    }
139}
140
141impl From<std::string::FromUtf8Error> for Error {
142    fn from(err: std::string::FromUtf8Error) -> Self {
143        Error::Other(err.to_string())
144    }
145}