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    /// Unexpected protocol error. The String contains the reason.
17    ProtocolError(String),
18    /// Parameter subsystem error. The String contains the reason.
19    ParamError(String),
20    /// Log Subsystem error. The String contains the reason.
21    LogError(String),
22    /// [Value](crate::Value) conversion error. The String contains the reason.
23    ConversionError(String),
24    /// Crazyflie link configuration error. Returns the [error from the Link](crazyflie_link::Error).
25    LinkError(crazyflie_link::Error),
26    /// The Crazyflie object is currently disconnected.
27    Disconnected,
28    /// Variable not found in TOC.
29    VariableNotFound,
30    /// Error with the async executors.
31    SystemError(String),
32    /// App channel packets should be no larger than [APPCHANNEL_MTU](crate::subsystems::platform::APPCHANNEL_MTU)
33    AppchannelPacketTooLarge,
34    /// Invalid argument passed to a function.
35    /// This error indicates that one or more arguments provided to a function are invalid.
36    InvalidArgument(String),
37    /// Operation timed out waiting for response.
38    Timeout,
39    /// Memory content malformed or not as expected. The String contains the reason.
40    MemoryError(String),
41    /// Invalid parameter provided to a function. The String contains the reason.
42    InvalidParameter(String),
43}
44
45impl std::fmt::Display for Error {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        match self {
48            Error::ProtocolVersionNotSupported => write!(f, "Protocol version not supported"),
49            Error::ProtocolError(msg) => write!(f, "Protocol error: {}", msg),
50            Error::ParamError(msg) => write!(f, "Parameter error: {}", msg),
51            Error::LogError(msg) => write!(f, "Log error: {}", msg),
52            Error::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
53            Error::LinkError(e) => write!(f, "Link error: {}", e),
54            Error::Disconnected => write!(f, "Disconnected"),
55            Error::VariableNotFound => write!(f, "Variable not found"),
56            Error::SystemError(msg) => write!(f, "System error: {}", msg),
57            Error::AppchannelPacketTooLarge => write!(f, "Appchannel packet too large"),
58            Error::InvalidArgument(msg) => write!(f, "Invalid argument: {}", msg),
59            Error::Timeout => write!(f, "Operation timed out"),
60            Error::MemoryError(msg) => write!(f, "Memory error: {}", msg),
61            Error::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
62        }
63    }
64}
65
66impl std::error::Error for Error {}
67
68impl From<TryFromSliceError> for Error {
69    fn from(e: TryFromSliceError) -> Self {
70        Self::ConversionError(format!("{:?}", e))
71    }
72}
73
74impl From<crazyflie_link::Error> for Error {
75    fn from(error: crazyflie_link::Error) -> Self {
76        Self::LinkError(error)
77    }
78}
79
80impl From<SpawnError> for Error {
81    fn from(error: SpawnError) -> Self {
82        Self::SystemError(format!("{}", error))
83    }
84}
85
86impl From<flume::RecvError> for Error {
87    fn from(_: flume::RecvError) -> Self {
88        self::Error::Disconnected
89    }
90}
91
92impl From<flume::SendError<Packet>> for Error {
93    fn from(_: flume::SendError<Packet>) -> Self {
94        self::Error::Disconnected
95    }
96}