use std::error::Error as StdError;
use std::fmt::{self, Error as FormatError};
use std::io::Error as IoError;
#[cfg(feature = "http")]
use reqwest::{header::InvalidHeaderValue, Error as ReqwestError};
#[cfg(feature = "gateway")]
use tokio_tungstenite::tungstenite::error::Error as TungsteniteError;
use tracing::instrument;
#[cfg(feature = "client")]
use crate::client::ClientError;
#[cfg(feature = "gateway")]
use crate::gateway::GatewayError;
#[cfg(feature = "http")]
use crate::http::HttpError;
use crate::internal::prelude::*;
use crate::json::JsonError;
use crate::model::ModelError;
pub type Result<T, E = Error> = StdResult<T, E>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Decode(&'static str, Value),
Format(FormatError),
Io(IoError),
#[cfg_attr(not(feature = "simd_json"), doc = "An error from the [`serde_json`] crate.")]
#[cfg_attr(feature = "simd_json", doc = "An error from the [`simd_json`] crate.")]
Json(JsonError),
Model(ModelError),
ExceededLimit(String, u32),
NotInRange(&'static str, u64, u64, u64),
Other(&'static str),
Url(String),
#[cfg(feature = "client")]
Client(ClientError),
#[cfg(feature = "gateway")]
Gateway(GatewayError),
#[cfg(feature = "http")]
Http(HttpError),
#[cfg(feature = "gateway")]
Tungstenite(TungsteniteError),
}
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)
}
}
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<ModelError> for Error {
fn from(e: ModelError) -> Error {
Error::Model(e)
}
}
#[cfg(feature = "gateway")]
impl From<TungsteniteError> for Error {
fn from(e: TungsteniteError) -> Error {
Error::Tungstenite(e)
}
}
#[cfg(feature = "http")]
impl From<HttpError> for Error {
fn from(e: HttpError) -> Error {
Error::Http(e)
}
}
#[cfg(feature = "http")]
impl From<InvalidHeaderValue> for Error {
fn from(e: InvalidHeaderValue) -> Error {
HttpError::InvalidHeader(e).into()
}
}
#[cfg(feature = "http")]
impl From<ReqwestError> for Error {
fn from(e: ReqwestError) -> Error {
HttpError::Request(e).into()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Decode(msg, _) | Self::Other(msg) => f.write_str(msg),
Self::ExceededLimit(..) => f.write_str("Input exceeded a limit"),
Self::NotInRange(..) => f.write_str("Input is not in the specified range"),
Self::Format(inner) => fmt::Display::fmt(&inner, f),
Self::Io(inner) => fmt::Display::fmt(&inner, f),
Self::Json(inner) => fmt::Display::fmt(&inner, f),
Self::Model(inner) => fmt::Display::fmt(&inner, f),
Self::Url(msg) => f.write_str(msg),
#[cfg(feature = "client")]
Self::Client(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "gateway")]
Self::Gateway(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "http")]
Self::Http(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "gateway")]
Self::Tungstenite(inner) => fmt::Display::fmt(&inner, f),
}
}
}
impl StdError for Error {
#[instrument]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Format(inner) => Some(inner),
Self::Io(inner) => Some(inner),
Self::Json(inner) => Some(inner),
Self::Model(inner) => Some(inner),
#[cfg(feature = "client")]
Self::Client(inner) => Some(inner),
#[cfg(feature = "http")]
Self::Http(inner) => Some(inner),
#[cfg(feature = "gateway")]
Self::Tungstenite(inner) => Some(inner),
_ => None,
}
}
}