rpi_sim868/
error.rs

1/// returned by [`Error::kind`] method.
2pub enum ErrorKind {
3    GnssModuleOff,
4    GnssNotFixed,
5    GnssProblem,
6    GprsApnConfigSetFailed,
7    GprsConnectionCloseFailed,
8    GprsConnectionOpenFailed,
9    GprsHttpRequestFailed,
10    GprsNoConnection,
11    HatAlreadyOff,
12    HatAlreadyOn,
13    JsonSerialisationFailed,
14    NotResolved,
15    PhoneCallNotAnswered,
16    PhoneCallNotCalled,
17    PhoneCallNotEnded,
18    RequestBodyParsingFailed,
19    SmsNotSent,
20    SmsProblemWithReadingMessages,
21    SmsProblemWithSettingTextMode,
22    SmsRemoveMessageFailed,
23    TokioJoinError,
24    Uart,
25    UrlParse,
26}
27
28/// RPi SIM868 Error enum.
29#[derive(Debug)]
30pub enum Error {
31    GnssModuleOff,
32    GnssNotFixed,
33    GnssProblem,
34    GprsApnConfigSetFailed,
35    GprsConnectionCloseFailed,
36    GprsConnectionOpenFailed,
37    GprsHttpRequestFailed,
38    GprsNoConnection,
39    HatAlreadyOff,
40    HatAlreadyOn,
41    JsonSerialisationFailed(serde_json::Error),
42    NotResolved,
43    PhoneCallNotAnswered,
44    PhoneCallNotCalled,
45    PhoneCallNotEnded,
46    RequestBodyParsingFailed(serde_url_params::Error),
47    SmsNotSent,
48    SmsProblemWithReadingMessages,
49    SmsProblemWithSettingTextMode,
50    SmsRemoveMessageFailed,
51    TokioJoinError(tokio::task::JoinError),
52    Uart(rppal::uart::Error),
53    UrlParse(url::ParseError),
54}
55
56impl std::fmt::Display for Error {
57    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
58        match self {
59            Error::GnssModuleOff => write!(f, "GNSS - module is off."),
60            Error::GnssNotFixed => write!(f, "GNSS - position is not fixed - check GSM antenna."),
61            Error::GnssProblem => write!(f, "GNSS - problem with the module."),
62            Error::GprsApnConfigSetFailed => write!(f, "GPRS - setting APN Configuration has failed."),
63            Error::GprsConnectionCloseFailed => write!(f, "GPRS - closing the connection has failed."),
64            Error::GprsConnectionOpenFailed => write!(f, "GPRS - opening the connection has failed. Make sure you provide valid APN configuration during sim868.gprs.init call."),
65            Error::GprsHttpRequestFailed => write!(f, "GPRS - HTTP request has failed."),
66            Error::GprsNoConnection => write!(f, "GPRS - no connection to the network."),
67            Error::HatAlreadyOff => write!(f, "HAT - already switched off."),
68            Error::HatAlreadyOn => write!(f, "HAT - already switched on."),
69            Error::JsonSerialisationFailed(ref err) => write!(f, "Object has failed when serialising to JSON: {}", err),
70            Error::NotResolved => write!(f, "Task NotResolved - please check if the hat is switched on."),
71            Error::PhoneCallNotAnswered => write!(f, "Phone - there was an error while trying to answer the call."),
72            Error::PhoneCallNotCalled => write!(f, "Phone - there was an error while trying to make a call - please check the network strength."),
73            Error::PhoneCallNotEnded => write!(f, "Phone - there was an error while trying to end a call - it could end previously eg. other side has hanged up."),
74            Error::RequestBodyParsingFailed(ref err) => write!(f, "Request body parsing has failed: {}", err),
75            Error::SmsNotSent => write!(f, "SMS - there was an error while trying to send an SMS - please check the network strength."),
76            Error::SmsProblemWithReadingMessages => write!(f, "SMS - problem with reading the messages."),
77            Error::SmsProblemWithSettingTextMode => write!(f, "SMS - problem with setting the text mode."),
78            Error::SmsRemoveMessageFailed => write!(f, "SMS - problem with removing the message/s."),
79            Error::TokioJoinError(ref err) => write!(f, "Tokio task join error: {}", err),
80            Error::Uart(ref err) => write!(f, "Uart error: {}", err),
81            Error::UrlParse(ref err) => write!(f, "URL parsing error: {}", err),
82        }
83    }
84}
85
86impl std::error::Error for Error {}
87
88impl Error {
89    pub fn kind(&self) -> ErrorKind {
90        match self {
91            Error::GnssModuleOff => ErrorKind::GnssModuleOff,
92            Error::GnssNotFixed => ErrorKind::GnssNotFixed,
93            Error::GnssProblem => ErrorKind::GnssProblem,
94            Error::GprsApnConfigSetFailed => ErrorKind::GprsApnConfigSetFailed,
95            Error::GprsConnectionCloseFailed => ErrorKind::GprsConnectionCloseFailed,
96            Error::GprsConnectionOpenFailed => ErrorKind::GprsConnectionOpenFailed,
97            Error::GprsHttpRequestFailed => ErrorKind::GprsHttpRequestFailed,
98            Error::GprsNoConnection => ErrorKind::GprsNoConnection,
99            Error::HatAlreadyOff => ErrorKind::HatAlreadyOff,
100            Error::HatAlreadyOn => ErrorKind::HatAlreadyOn,
101            Error::JsonSerialisationFailed(ref _e) => ErrorKind::JsonSerialisationFailed,
102            Error::NotResolved => ErrorKind::NotResolved,
103            Error::PhoneCallNotAnswered => ErrorKind::PhoneCallNotAnswered,
104            Error::PhoneCallNotCalled => ErrorKind::PhoneCallNotCalled,
105            Error::PhoneCallNotEnded => ErrorKind::PhoneCallNotEnded,
106            Error::RequestBodyParsingFailed(ref _e) => ErrorKind::RequestBodyParsingFailed,
107            Error::SmsNotSent => ErrorKind::SmsNotSent,
108            Error::SmsProblemWithReadingMessages => ErrorKind::SmsProblemWithReadingMessages,
109            Error::SmsProblemWithSettingTextMode => ErrorKind::SmsProblemWithSettingTextMode,
110            Error::SmsRemoveMessageFailed => ErrorKind::SmsRemoveMessageFailed,
111            Error::TokioJoinError(ref _e) => ErrorKind::TokioJoinError,
112            Error::Uart(ref _e) => ErrorKind::Uart,
113            Error::UrlParse(ref _e) => ErrorKind::UrlParse,
114        }
115    }
116}
117
118impl From<rppal::uart::Error> for Error {
119    fn from(err: rppal::uart::Error) -> Error {
120        Error::Uart(err)
121    }
122}
123
124impl From<url::ParseError> for Error {
125    fn from(err: url::ParseError) -> Error {
126        Error::UrlParse(err)
127    }
128}
129
130impl From<serde_url_params::Error> for Error {
131    fn from(err: serde_url_params::Error) -> Error {
132        Error::RequestBodyParsingFailed(err)
133    }
134}
135
136impl From<serde_json::Error> for Error {
137    fn from(err: serde_json::Error) -> Error {
138        Error::JsonSerialisationFailed(err)
139    }
140}
141
142impl From<tokio::task::JoinError> for Error {
143    fn from(err: tokio::task::JoinError) -> Error {
144        Error::TokioJoinError(err)
145    }
146}