sublime_standard_tools/error/
mod.rs

1//! # Error handling for `sublime_standard_tools`
2//!
3//! ## What
4//! This module provides comprehensive error types for various operations within
5//! the crate. It implements specific error types for different domains, such as
6//! filesystem operations, command execution, and project management.
7//!
8//! ## How
9//! Each domain has its own error type (e.g., `FileSystemError`) that implements
10//! the `Error` trait from the standard library and uses the `thiserror` crate
11//! for concise error definitions. Result type aliases are provided for convenience.
12//!
13//! ## Why
14//! A structured approach to error handling enables callers to handle errors
15//! appropriately based on their type and context, improving error reporting and
16//! recovery strategies. The consistent pattern makes error handling predictable
17//! across the crate.
18
19mod command;
20mod config;
21mod filesystem;
22mod monorepo;
23mod recovery;
24mod traits;
25mod workspace;
26
27#[cfg(test)]
28mod tests;
29
30use core::result::Result as CoreResult;
31use thiserror::Error as ThisError;
32
33// Re-export error types
34pub use command::{CommandError, CommandResult};
35pub use config::{ConfigError, ConfigResult};
36pub use filesystem::FileSystemError;
37pub use monorepo::{MonorepoError, MonorepoResult};
38pub use recovery::{ErrorRecoveryManager, LogLevel, RecoveryResult, RecoveryStrategy};
39pub use traits::ErrorContext;
40pub use workspace::{WorkspaceError, WorkspaceResult};
41
42/// Result type for filesystem operations.
43///
44/// This is a convenience type alias for Results with `FileSystemError`.
45///
46/// # Examples
47///
48/// ```
49/// use sublime_standard_tools::error::{FileSystemResult, FileSystemError};
50/// use std::path::PathBuf;
51///
52/// fn read_config(path: &str) -> FileSystemResult<String> {
53///     if path.is_empty() {
54///         return Err(FileSystemError::Validation {
55///             path: PathBuf::from(path),
56///             reason: "Empty path".to_string(),
57///         });
58///     }
59///     // Actual implementation would read the file
60///     Ok("sample config".to_string())
61/// }
62/// ```
63pub type FileSystemResult<T> = CoreResult<T, FileSystemError>;
64
65/// General error type for the standard tools library.
66///
67/// This enum serves as a composite error type that aggregates all domain-specific
68/// errors from the crate into a single error type. This allows for simplified error
69/// handling in consumer code that may deal with multiple domains.
70///
71/// # Examples
72///
73/// ```
74/// use sublime_standard_tools::error::{Error, FileSystemError, MonorepoError};
75/// use std::path::PathBuf;
76///
77/// // Creating an error from a filesystem error
78/// let fs_error = FileSystemError::NotFound { path: PathBuf::from("/missing/file.txt") };
79/// let error: Error = fs_error.into();
80///
81/// // Creating an error from a monorepo error
82/// let monorepo_error = MonorepoError::ManagerNotFound;
83/// let error: Error = monorepo_error.into();
84///
85/// // Using in a function that could have multiple error sources
86/// fn complex_operation() -> sublime_standard_tools::error::Result<()> {
87///     // This could return either a FileSystem or Monorepo error
88///     // Both will be automatically converted to the Error enum
89///     Ok(())
90/// }
91/// ```
92#[derive(ThisError, Debug, Clone)]
93pub enum Error {
94    /// Monorepo-related error.
95    #[error("Monorepo execution error")]
96    Monorepo(#[from] MonorepoError),
97    /// Filesystem-related error.
98    #[error("FileSystem execution error")]
99    FileSystem(#[from] FileSystemError),
100    /// Workspace-related error.
101    #[error("Workspace execution error")]
102    Workspace(#[from] WorkspaceError),
103    /// Command-related error.
104    #[error("Command execution error")]
105    Command(#[from] CommandError),
106    /// Configuration-related error.
107    #[error("Configuration error")]
108    Config(#[from] ConfigError),
109    /// General purpose errors with a custom message.
110    #[error("Operation error: {0}")]
111    Operation(String),
112}
113
114impl Error {
115    /// Creates a new operational error.
116    pub fn operation(message: impl Into<String>) -> Self {
117        Self::Operation(message.into())
118    }
119}
120
121impl AsRef<str> for Error {
122    fn as_ref(&self) -> &str {
123        match self {
124            Error::Monorepo(_) => "Error::Monorepo",
125            Error::FileSystem(_) => "Error::FileSystem",
126            Error::Workspace(_) => "Error::Workspace",
127            Error::Command(_) => "Error::Command",
128            Error::Config(_) => "Error::Config",
129            Error::Operation(_) => "Error::Operation",
130        }
131    }
132}
133
134/// Result type for general operations in the standard tools library.
135///
136/// This is a convenience type alias for Results with the composite Error type.
137/// It simplifies error handling when functions may return errors from various domains.
138///
139/// # Examples
140///
141/// ```
142/// use sublime_standard_tools::error::{Result, Error, FileSystemError};
143/// use std::path::PathBuf;
144///
145/// fn process_project_files(root_dir: &str) -> Result<Vec<String>> {
146///     if root_dir.is_empty() {
147///         return Err(FileSystemError::Validation {
148///             path: PathBuf::from(root_dir),
149///             reason: "Empty directory path".to_string(),
150///         }.into());
151///     }
152///     // Implementation that might return various error types
153///     Ok(vec!["file1.txt".to_string(), "file2.txt".to_string()])
154/// }
155/// ```
156pub type Result<T> = CoreResult<T, Error>;