diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..f7796b8 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,64 @@ +#[derive(Debug)] +pub enum TdlibSysError { + LogLevelOutOfBounds(u16), + Nul(usize), + UtfDecode, + CannotSetLogFile, +} + +impl std::error::Error for TdlibSysError {} + +impl std::convert::From<std::ffi::NulError> for TdlibSysError { + fn from(nul_err: std::ffi::NulError) -> Self { + Self::Nul(nul_err.nul_position()) + } +} + +impl std::fmt::Display for TdlibSysError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use TdlibSysError::*; + match self { + LogLevelOutOfBounds(lvl) => { + write!(f, "Log level should be less than 1024. Found {}", lvl) + } + Nul(pos) => write!(f, "String contains nul byte at pos: {}", pos), + UtfDecode => write!(f, "String is not utf8 encoded"), + CannotSetLogFile => write!(f, "Could not set log file"), + } + } +} + +#[derive(Debug, thiserror::Error)] +pub enum TgError { + /// Something went wrong, when (de)serializing shit + #[error("Something went wrong, when (de)serializing shit: {}", .0)] + Serde(#[from] serde_json::Error), + + /// tdlib-sys error + #[error("tdlib-sys error: {}", .0)] + TdSys(#[from] TdlibSysError), + + #[error("@type must not be in your structure used as Request")] + HasTypeInJson, + + #[error("@type must be string in JsonValue used as Request")] + HasNoTypeInJson, + + #[error("json is invalid: {}", .0)] + InvalidJson(String), + + #[error("core channel has been closed unexpectedly")] + ChannelClosed, + + #[error("telegram replied with error code: {code} and message: {msg}")] + TelegramError { code: i32, msg: String }, +} + +impl<T> std::convert::From<crossbeam::channel::SendError<T>> for TgError { + fn from(_: crossbeam::channel::SendError<T>) -> Self { + Self::ChannelClosed + } +} + +pub type Error = TgError; +pub type Result<T> = std::result::Result<T, Error>; |