discord-rpc-client/src/error.rs

69 lines
1.8 KiB
Rust
Raw Permalink Normal View History

2022-02-03 09:52:53 -05:00
use serde_json::Error as JsonError;
use std::{
error::Error as StdError,
2022-02-03 09:52:53 -05:00
fmt::{self, Display, Formatter},
io::Error as IoError,
result::Result as StdResult,
sync::mpsc::RecvTimeoutError as ChannelTimeout,
};
use tokio::sync::mpsc::error::SendError;
2022-02-03 09:52:53 -05:00
use crate::models::Message;
2018-03-29 16:57:00 -04:00
#[derive(Debug)]
pub enum Error {
IoError(IoError),
JsonError(JsonError),
Timeout(ChannelTimeout),
2018-03-29 16:57:00 -04:00
Conversion,
SubscriptionFailed,
ConnectionClosed,
2022-02-03 09:52:53 -05:00
ConnectionClosedWhileSending(Message),
Busy,
2018-03-29 16:57:00 -04:00
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2022-02-03 09:52:53 -05:00
match self {
Self::Conversion => f.write_str("Failed to convert values"),
Self::SubscriptionFailed => f.write_str("Failed to subscribe to event"),
Self::ConnectionClosed => f.write_str("Connection closed"),
Self::ConnectionClosedWhileSending(msg) => {
write!(f, "Connection closed while sending {:?}", msg)
}
2022-02-03 09:52:53 -05:00
Self::Busy => f.write_str("A resource was busy"),
Self::IoError(err) => write!(f, "{}", err),
Self::JsonError(err) => write!(f, "{}", err),
Self::Timeout(err) => write!(f, "{}", err),
2018-03-29 16:57:00 -04:00
}
}
}
2022-02-03 09:52:53 -05:00
impl StdError for Error {}
2018-03-29 16:57:00 -04:00
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
2022-02-03 09:52:53 -05:00
Self::IoError(err)
}
}
impl From<JsonError> for Error {
fn from(err: JsonError) -> Self {
2022-02-03 09:52:53 -05:00
Self::JsonError(err)
}
}
impl From<ChannelTimeout> for Error {
fn from(err: ChannelTimeout) -> Self {
2022-02-03 09:52:53 -05:00
Self::Timeout(err)
}
}
impl From<SendError<Message>> for Error {
fn from(err: SendError<Message>) -> Self {
Self::ConnectionClosedWhileSending(err.0)
2018-03-29 16:57:00 -04:00
}
}
pub type Result<T, E = Error> = StdResult<T, E>;