CommonLibrary/Error/
CommonError.rs

1//! # CommonError Enum
2//!
3//! Defines the universal, structured error enum for the entire application
4//! ecosystem.
5
6use std::path::PathBuf;
7
8use serde::{Deserialize, Serialize};
9use thiserror::Error;
10
11/// A common error enum for the application, encompassing all major categories
12/// of failures that can occur during the execution of effects or other
13// operations.
14#[derive(Error, Debug, Clone, Serialize, Deserialize)]
15pub enum CommonError {
16	// --- FileSystem Errors ---
17	#[error("FileSystem I/O error for '{Path}': {Description}")]
18	FileSystemIO { Path:PathBuf, Description:String },
19
20	#[error("Resource not found: {0}")]
21	FileSystemNotFound(PathBuf),
22
23	#[error("Permission denied for operation on '{Path}': {Reason}")]
24	FileSystemPermissionDenied { Path:PathBuf, Reason:String },
25
26	#[error("Resource already exists: {0}")]
27	FileSystemFileExists(PathBuf),
28
29	#[error("Path is not a directory: {0}")]
30	FileSystemNotADirectory(PathBuf),
31
32	#[error("Path is a directory (expected a file): {0}")]
33	FileSystemIsADirectory(PathBuf),
34
35	// --- Configuration Errors ---
36	#[error("Configuration update error for key '{Key}': {Description}")]
37	ConfigurationUpdate { Key:String, Description:String },
38
39	#[error("Configuration load error: {Description}")]
40	ConfigurationLoad { Description:String },
41
42	// --- General Application Errors ---
43	#[error("Invalid argument '{ArgumentName}': {Reason}")]
44	InvalidArgument { ArgumentName:String, Reason:String },
45
46	#[error("Feature not implemented: {FeatureName}")]
47	NotImplemented { FeatureName:String },
48
49	#[error("Internal state access error (e.g., lock poisoned): {Context}")]
50	StateLockPoisoned { Context:String },
51
52	#[error("Inter-process communication error: {Description}")]
53	IPCError { Description:String },
54
55	#[error("Access denied: {Reason}")]
56	AccessDenied { Reason:String },
57
58	// --- Command System Errors ---
59	#[error("Command '{CommandIdentifier}' execution failed: {Reason}")]
60	CommandExecution { CommandIdentifier:String, Reason:String },
61
62	#[error("Command '{Identifier}' not found")]
63	CommandNotFound { Identifier:String },
64
65	#[error("Feature '{FeatureName}' is not available")]
66	FeatureNotAvailable { FeatureName:String },
67
68	// --- Language Feature Provider Errors ---
69	#[error("Language provider registration failed for '{ProviderType}': {Reason}")]
70	ProviderRegistration { ProviderType:String, Reason:String },
71
72	#[error("Language provider '{ProviderIdentifier}' invocation failed: {Reason}")]
73	ProviderInvocation { ProviderIdentifier:String, Reason:String },
74
75	// --- Test Framework Errors ---
76	#[error("Test run '{RunIdentifier}' not found")]
77	TestRunNotFound { RunIdentifier:String },
78
79	#[error("Test controller '{ControllerIdentifier}' not found")]
80	TestControllerNotFound { ControllerIdentifier:String },
81
82	// --- TreeView Errors (New) ---
83	#[error("TreeView provider not found for view ID '{ViewIdentifier}'")]
84	TreeViewProviderNotFound { ViewIdentifier:String },
85
86	#[error("Webview '{Handle}' not found")]
87	WebviewNotFound { Handle:String },
88
89	// --- Other Specific Errors ---
90	#[error("External service '{ServiceName}' failed: {Description}")]
91	ExternalServiceError { ServiceName:String, Description:String },
92
93	#[error("Secret access for key '{Key}' failed: {Reason}")]
94	SecretsAccess { Key:String, Reason:String },
95
96	#[error("UserInterface interaction failed: {Reason}")]
97	UserInterfaceInteraction { Reason:String },
98
99	#[error("Serialization or Deserialization error: {Description}")]
100	SerializationError { Description:String },
101
102	// --- Catch-all Unknown Error ---
103	#[error("An unknown internal error occurred: {Description}")]
104	Unknown { Description:String },
105}
106
107impl CommonError {
108	/// Creates a `CommonError` from a standard `std::io::Error`, mapping common
109	/// I/O error kinds to our specific FileSystem error variants for better
110	/// contextualization.
111	pub fn FromStandardIOError(IOError:std::io::Error, Path:PathBuf, OperationContext:&str) -> Self {
112		let Description = IOError.to_string();
113
114		match IOError.kind() {
115			std::io::ErrorKind::NotFound => CommonError::FileSystemNotFound(Path),
116
117			std::io::ErrorKind::PermissionDenied => {
118				CommonError::FileSystemPermissionDenied { Path, Reason:Description }
119			},
120
121			std::io::ErrorKind::AlreadyExists => CommonError::FileSystemFileExists(Path),
122
123			_ => {
124				CommonError::FileSystemIO {
125					Path,
126
127					Description:format!("Operation '{}' failed: {}", OperationContext, Description),
128				}
129			},
130		}
131	}
132}
133
134/// Converts a `serde_json::Error` into a `CommonError::SerializationError`.
135impl From<serde_json::Error> for CommonError {
136	fn from(SerdeError:serde_json::Error) -> Self {
137		CommonError::SerializationError { Description:SerdeError.to_string() }
138	}
139}
140
141/// Converts a `tauri::Error` into a `CommonError`. This is useful for
142/// handling errors from Tauri's APIs within our effect system.
143impl From<tauri::Error> for CommonError {
144	fn from(TauriError:tauri::Error) -> Self { CommonError::UserInterfaceInteraction { Reason:TauriError.to_string() } }
145}