Mountain/Error/
IPCError.rs1use std::{error::Error as StdError, fmt};
7
8use serde::{Deserialize, Serialize};
9
10use super::CoreError::{ErrorContext, ErrorKind, ErrorSeverity, MountainError, Result};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub enum IPCError {
15 ConnectionFailed { context:ErrorContext, source:Option<String> },
17 MessageSendFailed { context:ErrorContext, message_id:Option<String> },
19 MessageReceiveFailed { context:ErrorContext, source:Option<String> },
21 InvalidMessageFormat { context:ErrorContext, raw_message:Option<String> },
23 Timeout { context:ErrorContext, operation:Option<String>, timeout_ms:u64 },
25 PermissionDenied { context:ErrorContext, required_permission:Option<String> },
27 ServiceUnavailable { context:ErrorContext, service_name:Option<String> },
29 QueueOverflow { context:ErrorContext, queue_size:usize },
31}
32
33impl IPCError {
34 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 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 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 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 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 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}