1use std::time::Duration;
2use thiserror::Error;
3use tokio::sync::mpsc;
4
5#[derive(Error, Debug, Default)]
7pub enum DriverError {
8 #[error("Service unavailable")]
9 #[default]
10 ServiceUnavailable,
11 #[error("Validation error: {0}")]
12 ValidationError(String),
13 #[error("Execution error: {0}")]
14 ExecutionError(String),
15 #[error("Configuration error: {0}")]
16 ConfigurationError(String),
17 #[error("Initialization error: {0}")]
18 InitializationError(String),
19 #[error("Invalid state error: {0}")]
20 InvalidStateError(String),
21 #[error("Codec error: {0}")]
22 CodecError(String),
23 #[error("Read/Write timeout")]
24 Timeout(Duration),
25 #[error("Planner error: {0}")]
26 PlannerError(String),
27 #[error("Session error: {0}")]
28 SessionError(String),
29 #[error("Subscription error: {0}")]
30 SubscriptionError(String),
31 #[error("Northward error: {0}")]
32 NorthwardError(String),
33 #[error("LoadError error: {0}")]
34 LoadError(String),
35 #[error("Invalid entity: {0}")]
36 InvalidEntity(String),
37}
38
39#[derive(Error, Debug)]
41pub enum NorthwardError {
42 #[error("Authentication failed for platform '{platform}': {reason}")]
44 AuthenticationFailed { platform: String, reason: String },
45
46 #[error("Failed to publish message to platform '{platform}': {reason}")]
48 PublishFailed { platform: String, reason: String },
49
50 #[error("Failed to subscribe to topic '{topic}' on platform '{platform}': {reason}")]
52 SubscriptionFailed {
53 platform: String,
54 topic: String,
55 reason: String,
56 },
57
58 #[error("Serialization failed: {reason}")]
60 SerializationError { reason: String },
61
62 #[error("Deserialization failed: {reason}")]
64 DeserializationError { reason: String },
65
66 #[error("Configuration error: {message}")]
68 ConfigurationError { message: String },
69
70 #[error("Data send error: {message}")]
72 DataSendError { message: String },
73
74 #[error("Operation timed out after {timeout_ms}ms: {operation}")]
76 Timeout { timeout_ms: u64, operation: String },
77
78 #[error("Circuit breaker is open for platform '{platform}'")]
80 CircuitBreakerOpen { platform: String },
81
82 #[error("Platform '{platform}' not found or not configured")]
84 PlatformNotFound { platform: String },
85
86 #[error("Invalid message format: {reason}")]
88 InvalidMessageFormat { reason: String },
89
90 #[error("TLS error for platform '{platform}': {reason}")]
92 TlsError { platform: String, reason: String },
93
94 #[error("MQTT protocol error: {reason}")]
96 MqttError { reason: String },
97
98 #[error("Resource exhausted: {resource}")]
100 ResourceExhausted { resource: String },
101
102 #[error("Operation not supported: {operation} for platform '{platform}'")]
104 OperationNotSupported { operation: String, platform: String },
105
106 #[error("Device provision failed for platform '{platform}': {reason}")]
108 ProvisionFailed { platform: String, reason: String },
109
110 #[error("Device credentials not found for platform '{platform}'")]
112 CredentialsNotFound { platform: String },
113
114 #[error("Device provision timeout after {timeout_ms}ms for platform '{platform}'")]
116 ProvisionTimeout { platform: String, timeout_ms: u64 },
117
118 #[error("Not connected to platform - operation requires active connection")]
120 NotConnected,
121
122 #[error("Invalid provision configuration for platform '{platform}': {reason}")]
124 InvalidProvisionConfig { platform: String, reason: String },
125
126 #[error("Northward manager queue is full (capacity reached)")]
128 QueueFull,
129
130 #[error("Load error: {0}")]
132 LoadError(String),
133
134 #[error("Not found: {entity}")]
136 NotFound { entity: String },
137
138 #[error("Gateway error: {reason}")]
140 GatewayError { reason: String },
141
142 #[error("Validation failed: {reason}")]
144 ValidationFailed { reason: String },
145
146 #[error("Runtime error: {reason}")]
148 RuntimeError { reason: String },
149
150 #[error("Storage error: {reason}")]
155 StorageError { reason: String },
156}
157
158impl From<serde_json::Error> for NorthwardError {
159 fn from(err: serde_json::Error) -> Self {
160 NorthwardError::SerializationError {
161 reason: err.to_string(),
162 }
163 }
164}
165
166impl<T> From<mpsc::error::SendError<T>> for NorthwardError {
167 fn from(err: mpsc::error::SendError<T>) -> Self {
168 NorthwardError::DataSendError {
169 message: format!("Channel send failed: {err}"),
170 }
171 }
172}