Common/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	// --- Command System Errors ---
56	#[error("Command '{CommandIdentifier}' execution failed: {Reason}")]
57	CommandExecution { CommandIdentifier:String, Reason:String },
58
59	#[error("Command '{Identifier}' not found")]
60	CommandNotFound { Identifier:String },
61
62	// --- Language Feature Provider Errors ---
63	#[error("Language provider registration failed for '{ProviderType}': {Reason}")]
64	ProviderRegistration { ProviderType:String, Reason:String },
65
66	#[error("Language provider '{ProviderIdentifier}' invocation failed: {Reason}")]
67	ProviderInvocation { ProviderIdentifier:String, Reason:String },
68
69	// --- TreeView Errors (New) ---
70	#[error("TreeView provider not found for view ID '{ViewIdentifier}'")]
71	TreeViewProviderNotFound { ViewIdentifier:String },
72
73	// --- Other Specific Errors ---
74	#[error("External service '{ServiceName}' failed: {Description}")]
75	ExternalServiceError { ServiceName:String, Description:String },
76
77	#[error("Secret access for key '{Key}' failed: {Reason}")]
78	SecretsAccess { Key:String, Reason:String },
79
80	#[error("UserInterface interaction failed: {Reason}")]
81	UserInterfaceInteraction { Reason:String },
82
83	#[error("Serialization or Deserialization error: {Description}")]
84	SerializationError { Description:String },
85
86	// --- Catch-all Unknown Error ---
87	#[error("An unknown internal error occurred: {Description}")]
88	Unknown { Description:String },
89}
90
91impl CommonError {
92	/// Creates a `CommonError` from a standard `std::io::Error`, mapping common
93	/// I/O error kinds to our specific FileSystem error variants for better
94	/// contextualization.
95	pub fn FromStandardIOError(IOError:std::io::Error, Path:PathBuf, OperationContext:&str) -> Self {
96		let Description = IOError.to_string();
97
98		match IOError.kind() {
99			std::io::ErrorKind::NotFound => CommonError::FileSystemNotFound(Path),
100
101			std::io::ErrorKind::PermissionDenied => {
102				CommonError::FileSystemPermissionDenied { Path, Reason:Description }
103			},
104
105			std::io::ErrorKind::AlreadyExists => CommonError::FileSystemFileExists(Path),
106
107			_ => {
108				CommonError::FileSystemIO {
109					Path,
110
111					Description:format!("Operation '{}' failed: {}", OperationContext, Description),
112				}
113			},
114		}
115	}
116}
117
118/// Converts a `serde_json::Error` into a `CommonError::SerializationError`.
119impl From<serde_json::Error> for CommonError {
120	fn from(SerdeError:serde_json::Error) -> Self {
121		CommonError::SerializationError { Description:SerdeError.to_string() }
122	}
123}
124
125/// Converts a `tauri::Error` into a `CommonError`. This is useful for
126/// handling errors from Tauri's APIs within our effect system.
127impl From<tauri::Error> for CommonError {
128	fn from(TauriError:tauri::Error) -> Self { CommonError::UserInterfaceInteraction { Reason:TauriError.to_string() } }
129}