huawei_modem/
errors.rs

1//! Error handling.
2use std::io;
3use futures::sync::oneshot::Canceled;
4use crate::pdu::MessageEncoding;
5use crate::at;
6use failure::Fail;
7
8/// An error either raised by a command implementation, or by the library itself.
9#[derive(Fail, Debug)]
10pub enum CommandError<T> where T: Fail {
11    /// An error in the implementation of some generic command.
12    #[fail(display = "Error in command: {}", _0)]
13    Command(#[cause] T),
14    /// An error raised by the library itself.
15    #[fail(display = "{}", _0)]
16    Huawei(#[cause] HuaweiError)
17}
18impl<T> From<HuaweiError> for CommandError<T> where T: Fail {
19    fn from(e: HuaweiError) -> CommandError<T> {
20        CommandError::Huawei(e)
21    }
22}
23/// Error `enum` for errors raised by this library.
24///
25/// Exhaustive matching is NOT guaranteed by the library API (!).
26#[derive(Fail, Debug)]
27pub enum HuaweiError {
28    /// The background future used to talk to the modem died, making any sort of interaction with
29    /// any library feature somewhat...difficult.
30    #[fail(display = "Failed to communicate with the background future (it's likely dead).")]
31    FutureDied,
32    /// An error from the modem itself.
33    #[fail(display = "Error from modem: {}", _0)]
34    AtError(#[cause] at::AtResultCode),
35    /// Some random I/O error.
36    #[fail(display = "An I/O error occurred: {}", _0)]
37    IoError(#[cause] io::Error),
38    /// An error parsing data from the modem.
39    #[fail(display = "There was an error parsing data.")]
40    ParseError(::nom::ErrorKind),
41    /// An indication that an `InformationResponse` of some form from the modem was expected, but
42    /// never provided.
43    #[fail(display = "Expected a {} response", _0)]
44    ExpectedResponse(String),
45    /// A type mismatch occured when parsing data from the modem.
46    #[fail(display = "Type mismatch when parsing reply")]
47    TypeMismatch,
48    /// A value provided by the modem was out of range.
49    #[fail(display = "Value out of range: {}", _0)]
50    ValueOutOfRange(at::AtValue),
51    /// An error occured parsing a PDU.
52    #[fail(display = "Invalid PDU: {}", _0)]
53    InvalidPdu(&'static str),
54    /// Unsupported user data encoding. The raw bytes are provided for your edification.
55    #[fail(display = "Data of unknown encoding {:?}: {:?}", _0, _1)]
56    UnsupportedEncoding(MessageEncoding, Vec<u8>),
57    /// This shouldn't be shown, and is designed to stop you matching on this `enum` exhaustively.
58    /// If you do that, yo' code gonna break!
59    #[fail(display = "[this should never be shown]")]
60    #[doc(hidden)]
61    __Nonexhaustive
62}
63impl From<io::Error> for HuaweiError {
64    fn from(e: io::Error) -> HuaweiError {
65        HuaweiError::IoError(e)
66    }
67}
68impl From<::nom::ErrorKind> for HuaweiError {
69    fn from(e: ::nom::ErrorKind) -> HuaweiError {
70        HuaweiError::ParseError(e)
71    }
72}
73impl From<Canceled> for HuaweiError {
74    fn from(_: Canceled) -> HuaweiError {
75        HuaweiError::FutureDied
76    }
77}
78/// Bog-standard result type alias.
79pub type HuaweiResult<T> = Result<T, HuaweiError>;