Mountain/Error/
IPCError.rs

1//! # IPC Error Types
2//!
3//! Provides IPC-specific error types for Mountain.
4//! Used for all IPC communication errors.
5
6use std::{error::Error as StdError, fmt};
7
8use serde::{Deserialize, Serialize};
9
10use super::CoreError::{ErrorContext, ErrorKind, ErrorSeverity, MountainError, Result};
11
12/// IPC-specific error types
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub enum IPCError {
15	/// Connection failed
16	ConnectionFailed { context:ErrorContext, source:Option<String> },
17	/// Message send failed
18	MessageSendFailed { context:ErrorContext, message_id:Option<String> },
19	/// Message receive failed
20	MessageReceiveFailed { context:ErrorContext, source:Option<String> },
21	/// Invalid message format
22	InvalidMessageFormat { context:ErrorContext, raw_message:Option<String> },
23	/// Timeout occurred
24	Timeout { context:ErrorContext, operation:Option<String>, timeout_ms:u64 },
25	/// Permission denied
26	PermissionDenied { context:ErrorContext, required_permission:Option<String> },
27	/// Service unavailable
28	ServiceUnavailable { context:ErrorContext, service_name:Option<String> },
29	/// Queue overflow
30	QueueOverflow { context:ErrorContext, queue_size:usize },
31}
32
33impl IPCError {
34	/// Get the error context
35	pub fn context(&self) -> &ErrorContext {
36		match self {
37			IPCError::ConnectionFailed { context, .. } => context,
38			IPCError::MessageSendFailed { context, .. } => context,
39			IPCError::MessageReceiveFailed { context, .. } => context,
40			IPCError::InvalidMessageFormat { context, .. } => context,
41			IPCError::Timeout { context, .. } => context,
42			IPCError::PermissionDenied { context, .. } => context,
43			IPCError::ServiceUnavailable { context, .. } => context,
44			IPCError::QueueOverflow { context, .. } => context,
45		}
46	}
47
48	/// Create a connection failed error
49	pub fn connection_failed(message:impl Into<String>) -> Self {
50		Self::ConnectionFailed {
51			context:ErrorContext::new(message)
52				.with_kind(ErrorKind::IPC)
53				.with_severity(ErrorSeverity::Error),
54			source:None,
55		}
56	}
57
58	/// Create a message send failed error
59	pub fn message_send_failed(message:impl Into<String>, message_id:Option<String>) -> Self {
60		Self::MessageSendFailed {
61			context:ErrorContext::new(message)
62				.with_kind(ErrorKind::IPC)
63				.with_severity(ErrorSeverity::Error),
64			message_id,
65		}
66	}
67
68	/// Create a timeout error
69	pub fn timeout(operation:impl Into<String>, timeout_ms:u64) -> Self {
70		let operation_str = operation.into();
71		Self::Timeout {
72			context:ErrorContext::new(format!("Operation timed out after {}ms", timeout_ms))
73				.with_kind(ErrorKind::IPC)
74				.with_severity(ErrorSeverity::Error)
75				.with_operation(operation_str.clone()),
76			operation:Some(operation_str),
77			timeout_ms,
78		}
79	}
80
81	/// Create a permission denied error
82	pub fn permission_denied(message:impl Into<String>, required_permission:Option<String>) -> Self {
83		Self::PermissionDenied {
84			context:ErrorContext::new(message)
85				.with_kind(ErrorKind::IPC)
86				.with_severity(ErrorSeverity::Critical),
87			required_permission,
88		}
89	}
90
91	/// Create a service unavailable error
92	pub fn service_unavailable(message:impl Into<String>, service_name:Option<String>) -> Self {
93		Self::ServiceUnavailable {
94			context:ErrorContext::new(message)
95				.with_kind(ErrorKind::IPC)
96				.with_severity(ErrorSeverity::Error),
97			service_name,
98		}
99	}
100}
101
102impl fmt::Display for IPCError {
103	fn fmt(&self, f:&mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.context()) }
104}
105
106impl StdError for IPCError {}
107
108impl From<IPCError> for MountainError {
109	fn from(err:IPCError) -> Self { MountainError::new(err.context().clone()).with_source(err.to_string()) }
110}