Skip to main content

crazyflie_lib/
error.rs

1use std::array::TryFromSliceError;
2
3use crazyflie_link::Packet;
4use futures::task::SpawnError;
5
6/// [Result] alias for return types of the crate API
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Error enum type
10#[derive(Debug)]
11pub enum Error {
12    /// Protocol version not supported, you need to update either the lib or the Crazyflie.
13    ///
14    /// see [the crate documentation](crate#compatibility) for more information.
15    ProtocolVersionNotSupported {
16        /// The minimum protocol version supported by this library
17        min_supported: u8,
18        /// The maximum protocol version supported by this library
19        max_supported: u8,
20        /// The protocol version found on the Crazyflie
21        found: u8,
22    },
23    /// Unexpected protocol error. The String contains the reason.
24    ProtocolError(String),
25    /// Parameter subsystem error. The String contains the reason.
26    ParamError(String),
27    /// Log Subsystem error. The String contains the reason.
28    LogError(String),
29    /// [Value](crate::Value) conversion error. The String contains the reason.
30    ConversionError(String),
31    /// Crazyflie link configuration error. Returns the [error from the Link](crazyflie_link::Error).
32    LinkError(crazyflie_link::Error),
33    /// The Crazyflie object is currently disconnected.
34    Disconnected,
35    /// Variable not found in TOC.
36    VariableNotFound,
37    /// Error with the async executors.
38    SystemError(String),
39    /// App channel packets should be no larger than [APPCHANNEL_MTU](crate::subsystems::platform::APPCHANNEL_MTU)
40    AppchannelPacketTooLarge,
41    /// Invalid argument passed to a function.
42    /// This error indicates that one or more arguments provided to a function are invalid.
43    InvalidArgument(String),
44    /// Operation timed out waiting for response.
45    Timeout,
46    /// Memory content malformed or not as expected. The String contains the reason.
47    MemoryError(String),
48    /// Invalid parameter provided to a function. The String contains the reason.
49    InvalidParameter(String),
50}
51
52impl std::fmt::Display for Error {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Error::ProtocolVersionNotSupported { min_supported, max_supported, found } => {
56                write!(f, "Protocol version not supported: supported range is {}-{}, found {}", min_supported, max_supported, found)
57            }
58            Error::ProtocolError(msg) => write!(f, "Protocol error: {}", msg),
59            Error::ParamError(msg) => write!(f, "Parameter error: {}", msg),
60            Error::LogError(msg) => write!(f, "Log error: {}", msg),
61            Error::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
62            Error::LinkError(e) => write!(f, "Link error: {}", e),
63            Error::Disconnected => write!(f, "Disconnected"),
64            Error::VariableNotFound => write!(f, "Variable not found"),
65            Error::SystemError(msg) => write!(f, "System error: {}", msg),
66            Error::AppchannelPacketTooLarge => write!(f, "Appchannel packet too large"),
67            Error::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
68            Error::Timeout => write!(f, "Operation timed out"),
69            Error::MemoryError(msg) => write!(f, "Memory error: {}", msg),
70            Error::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
71        }
72    }
73}
74
75impl std::error::Error for Error {}
76
77impl From<TryFromSliceError> for Error {
78    fn from(e: TryFromSliceError) -> Self {
79        Self::ConversionError(format!("{:?}", e))
80    }
81}
82
83impl From<crazyflie_link::Error> for Error {
84    fn from(error: crazyflie_link::Error) -> Self {
85        Self::LinkError(error)
86    }
87}
88
89impl From<SpawnError> for Error {
90    fn from(error: SpawnError) -> Self {
91        Self::SystemError(format!("{}", error))
92    }
93}
94
95impl From<flume::RecvError> for Error {
96    fn from(_: flume::RecvError) -> Self {
97        self::Error::Disconnected
98    }
99}
100
101impl From<flume::SendError<Packet>> for Error {
102    fn from(_: flume::SendError<Packet>) -> Self {
103        self::Error::Disconnected
104    }
105}