Skip to main content

ng_gateway_sdk/
error.rs

1use std::time::Duration;
2use thiserror::Error;
3use tokio::sync::mpsc;
4
5/// Driver specific errors
6#[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/// Northward communication specific errors
40#[derive(Error, Debug)]
41pub enum NorthwardError {
42    /// Authentication failed
43    #[error("Authentication failed for platform '{platform}': {reason}")]
44    AuthenticationFailed { platform: String, reason: String },
45
46    /// Message publishing failed
47    #[error("Failed to publish message to platform '{platform}': {reason}")]
48    PublishFailed { platform: String, reason: String },
49
50    /// Message subscription failed
51    #[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    /// Serialization error
59    #[error("Serialization failed: {reason}")]
60    SerializationError { reason: String },
61
62    /// Deserialization error
63    #[error("Deserialization failed: {reason}")]
64    DeserializationError { reason: String },
65
66    /// Configuration error
67    #[error("Configuration error: {message}")]
68    ConfigurationError { message: String },
69
70    /// Data send error
71    #[error("Data send error: {message}")]
72    DataSendError { message: String },
73
74    /// Timeout error
75    #[error("Operation timed out after {timeout_ms}ms: {operation}")]
76    Timeout { timeout_ms: u64, operation: String },
77
78    /// Circuit breaker open
79    #[error("Circuit breaker is open for platform '{platform}'")]
80    CircuitBreakerOpen { platform: String },
81
82    /// Platform not found
83    #[error("Platform '{platform}' not found or not configured")]
84    PlatformNotFound { platform: String },
85
86    /// Invalid message format
87    #[error("Invalid message format: {reason}")]
88    InvalidMessageFormat { reason: String },
89
90    /// TLS/SSL error
91    #[error("TLS error for platform '{platform}': {reason}")]
92    TlsError { platform: String, reason: String },
93
94    /// MQTT protocol error
95    #[error("MQTT protocol error: {reason}")]
96    MqttError { reason: String },
97
98    /// Resource exhausted (e.g., connection pool full)
99    #[error("Resource exhausted: {resource}")]
100    ResourceExhausted { resource: String },
101
102    /// Operation not supported
103    #[error("Operation not supported: {operation} for platform '{platform}'")]
104    OperationNotSupported { operation: String, platform: String },
105
106    /// Device provision failed
107    #[error("Device provision failed for platform '{platform}': {reason}")]
108    ProvisionFailed { platform: String, reason: String },
109
110    /// Device credentials not found
111    #[error("Device credentials not found for platform '{platform}'")]
112    CredentialsNotFound { platform: String },
113
114    /// Device provision timeout
115    #[error("Device provision timeout after {timeout_ms}ms for platform '{platform}'")]
116    ProvisionTimeout { platform: String, timeout_ms: u64 },
117
118    /// Not connected to platform
119    #[error("Not connected to platform - operation requires active connection")]
120    NotConnected,
121
122    /// Invalid provision configuration
123    #[error("Invalid provision configuration for platform '{platform}': {reason}")]
124    InvalidProvisionConfig { platform: String, reason: String },
125
126    /// Internal queue is full; backpressure signaled to upstream
127    #[error("Northward manager queue is full (capacity reached)")]
128    QueueFull,
129
130    /// Dynamic loading error
131    #[error("Load error: {0}")]
132    LoadError(String),
133
134    /// Entity not found in gateway runtime (non-hot-path).
135    #[error("Not found: {entity}")]
136    NotFound { entity: String },
137
138    /// Gateway-side execution error (non-hot-path), used by write-back protocols.
139    #[error("Gateway error: {reason}")]
140    GatewayError { reason: String },
141
142    /// Parameter/value validation failed (non-hot-path), used by write-back protocols.
143    #[error("Validation failed: {reason}")]
144    ValidationFailed { reason: String },
145
146    /// Runtime execution error within the plugin environment (e.g. task cancellation)
147    #[error("Runtime error: {reason}")]
148    RuntimeError { reason: String },
149
150    /// Host-owned storage error (DB, durable KV, etc.).
151    ///
152    /// # Notes
153    /// This is primarily used for control-plane persistence such as northward app extensions.
154    #[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}