use std::error::Error as StdError;
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Library(String),
Agent(String),
ApiKey(String),
Connection(String),
Timeout(String),
Config(String),
Disposed,
#[cfg(feature = "auth")]
Auth(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Library(msg) => write!(f, "Library error: {}", msg),
Error::Agent(msg) => write!(f, "Agent error: {}", msg),
Error::ApiKey(msg) => write!(f, "API key error: {}", msg),
Error::Connection(msg) => write!(f, "Connection error: {}", msg),
Error::Timeout(msg) => write!(f, "Timeout error: {}", msg),
Error::Config(msg) => write!(f, "Configuration error: {}", msg),
Error::Disposed => write!(f, "Agent has been disposed"),
#[cfg(feature = "auth")]
Error::Auth(msg) => write!(f, "Auth error: {}", msg),
}
}
}
impl StdError for Error {}
impl Error {
pub fn agent<S: Into<String>>(msg: S) -> Self {
Error::Agent(msg.into())
}
pub fn library<S: Into<String>>(msg: S) -> Self {
Error::Library(msg.into())
}
pub fn connection<S: Into<String>>(msg: S) -> Self {
Error::Connection(msg.into())
}
pub fn timeout<S: Into<String>>(msg: S) -> Self {
Error::Timeout(msg.into())
}
pub fn config<S: Into<String>>(msg: S) -> Self {
Error::Config(msg.into())
}
#[cfg(feature = "auth")]
pub fn auth<S: Into<String>>(msg: S) -> Self {
Error::Auth(msg.into())
}
}