Skip to main content

mikrotik_tokio/
error.rs

1//! Tokio-specific error types for `MikroTik` device operations.
2
3use std::io;
4
5use mikrotik_proto::error::{ConnectionError, LoginError, ProtocolError};
6use mikrotik_proto::response::TrapResponse;
7use thiserror::Error;
8
9/// Result type alias for `MikroTik` device operations.
10pub type DeviceResult<T> = Result<T, DeviceError>;
11
12/// Errors related to the device connection actor being unavailable.
13#[derive(Error, Debug, Clone, Copy)]
14pub enum ActorError {
15    /// Failed to send command because the actor's channel is closed.
16    #[error("Failed to send command: actor is unavailable (channel closed)")]
17    CommandSendFailed,
18
19    /// Login response was not received because the actor shut down during login.
20    #[error("Login response not received: actor shut down during login")]
21    LoginResponseLost,
22}
23
24/// Custom error type for `MikroTik` device operations.
25#[derive(Error, Debug)]
26pub enum DeviceError {
27    /// Connection related errors (TCP, network issues).
28    #[error("Connection error: {0}")]
29    Connection(#[from] io::Error),
30
31    /// Authentication failure.
32    #[error("Authentication failed: {response}")]
33    Authentication {
34        /// The trap response received from the device.
35        response: TrapResponse,
36    },
37
38    /// Actor unavailability errors.
39    #[error("Actor error: {0}")]
40    Actor(#[from] ActorError),
41
42    /// Protocol-level errors from the sans-IO layer.
43    #[error("Protocol error: {0}")]
44    Protocol(#[from] ProtocolError),
45
46    /// Connection state machine errors.
47    #[error("Connection state error: {0}")]
48    ConnectionState(#[from] ConnectionError),
49
50    /// Login errors.
51    #[error("Login error: {0}")]
52    Login(#[from] LoginError),
53
54    /// TLS handshake or protocol error.
55    #[cfg(feature = "tokio-tls")]
56    #[error("TLS error: {0}")]
57    Tls(#[from] rustls::Error),
58
59    /// The connection was closed by the remote device.
60    #[error("Connection closed by remote device")]
61    ConnectionClosed,
62}
63
64impl<T> From<tokio::sync::mpsc::error::SendError<T>> for DeviceError {
65    fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
66        DeviceError::Actor(ActorError::CommandSendFailed)
67    }
68}