discord-rpc-client/src/error.rs

69 lines
1.8 KiB
Rust

use serde_json::Error as JsonError;
use std::{
error::Error as StdError,
fmt::{self, Display, Formatter},
io::Error as IoError,
result::Result as StdResult,
sync::mpsc::RecvTimeoutError as ChannelTimeout,
};
use tokio::sync::mpsc::error::SendError;
use crate::models::Message;
#[derive(Debug)]
pub enum Error {
IoError(IoError),
JsonError(JsonError),
Timeout(ChannelTimeout),
Conversion,
SubscriptionFailed,
ConnectionClosed,
ConnectionClosedWhileSending(Message),
Busy,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
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)
}
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),
}
}
}
impl StdError for Error {}
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
Self::IoError(err)
}
}
impl From<JsonError> for Error {
fn from(err: JsonError) -> Self {
Self::JsonError(err)
}
}
impl From<ChannelTimeout> for Error {
fn from(err: ChannelTimeout) -> Self {
Self::Timeout(err)
}
}
impl From<SendError<Message>> for Error {
fn from(err: SendError<Message>) -> Self {
Self::ConnectionClosedWhileSending(err.0)
}
}
pub type Result<T, E = Error> = StdResult<T, E>;