use internal::prelude::*;
use model::ModelError;
use serde_json::Error as JsonError;
use std::{
error::Error as StdError,
fmt::{
self,
Display,
Error as FormatError
},
io::Error as IoError,
num::ParseIntError
};
#[cfg(feature = "hyper")]
use hyper::Error as HyperError;
#[cfg(feature = "native-tls")]
use native_tls::Error as TlsError;
#[cfg(feature = "voice")]
use opus::Error as OpusError;
#[cfg(feature = "websocket")]
use websocket::result::WebSocketError;
#[cfg(feature = "client")]
use client::ClientError;
#[cfg(feature = "gateway")]
use gateway::GatewayError;
#[cfg(feature = "http")]
use http::HttpError;
#[cfg(feature = "voice")]
use voice::VoiceError;
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub enum Error {
Decode(&'static str, Value),
Format(FormatError),
Io(IoError),
Json(JsonError),
Model(ModelError),
Num(ParseIntError),
ExceededLimit(String, u32),
Other(&'static str),
Url(String),
#[cfg(feature = "client")]
Client(ClientError),
#[cfg(feature = "gateway")]
Gateway(GatewayError),
#[cfg(feature = "http")]
Http(HttpError),
#[cfg(feature = "hyper")]
Hyper(HyperError),
#[cfg(feature = "native-tls")]
Tls(TlsError),
#[cfg(feature = "gateway")]
WebSocket(WebSocketError),
#[cfg(feature = "voice")]
Opus(OpusError),
#[cfg(feature = "voice")]
Voice(VoiceError),
}
impl From<FormatError> for Error {
fn from(e: FormatError) -> Error { Error::Format(e) }
}
#[cfg(feature = "gateway")]
impl From<GatewayError> for Error {
fn from(e: GatewayError) -> Error { Error::Gateway(e) }
}
#[cfg(feature = "hyper")]
impl From<HyperError> for Error {
fn from(e: HyperError) -> Error { Error::Hyper(e) }
}
impl From<IoError> for Error {
fn from(e: IoError) -> Error { Error::Io(e) }
}
impl From<JsonError> for Error {
fn from(e: JsonError) -> Error { Error::Json(e) }
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Error { Error::Num(e) }
}
impl From<ModelError> for Error {
fn from(e: ModelError) -> Error { Error::Model(e) }
}
#[cfg(feature = "voice")]
impl From<OpusError> for Error {
fn from(e: OpusError) -> Error { Error::Opus(e) }
}
#[cfg(feature = "native-tls")]
impl From<TlsError> for Error {
fn from(e: TlsError) -> Error { Error::Tls(e) }
}
#[cfg(feature = "gateway")]
impl From<WebSocketError> for Error {
fn from(e: WebSocketError) -> Error { Error::WebSocket(e) }
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Decode(msg, _) | Error::Other(msg) => msg,
Error::ExceededLimit(..) => "Input exceeded a limit",
Error::Format(ref inner) => inner.description(),
Error::Io(ref inner) => inner.description(),
Error::Json(ref inner) => inner.description(),
Error::Model(ref inner) => inner.description(),
Error::Num(ref inner) => inner.description(),
Error::Url(ref inner) => inner,
#[cfg(feature = "client")]
Error::Client(ref inner) => inner.description(),
#[cfg(feature = "gateway")]
Error::Gateway(ref inner) => inner.description(),
#[cfg(feature = "http")]
Error::Http(ref inner) => inner.description(),
#[cfg(feature = "http")]
Error::Hyper(ref inner) => inner.description(),
#[cfg(feature = "voice")]
Error::Opus(ref inner) => inner.description(),
#[cfg(feature = "native-tls")]
Error::Tls(ref inner) => inner.description(),
#[cfg(feature = "voice")]
Error::Voice(_) => "Voice error",
#[cfg(feature = "gateway")]
Error::WebSocket(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&StdError> {
match *self {
#[cfg(feature = "http")]
Error::Hyper(ref inner) => Some(inner),
Error::Json(ref inner) => Some(inner),
Error::Io(ref inner) => Some(inner),
#[cfg(feature = "gateway")]
Error::WebSocket(ref inner) => Some(inner),
_ => None,
}
}
}