1use std::error::Error as StdError;
2use std::fmt::{self, Error as FormatError};
3use std::io::Error as IoError;
4
5#[cfg(feature = "http")]
6use reqwest::{header::InvalidHeaderValue, Error as ReqwestError};
7#[cfg(feature = "gateway")]
8use tokio_tungstenite::tungstenite::error::Error as TungsteniteError;
9use tracing::instrument;
10
11#[cfg(feature = "client")]
12use crate::client::ClientError;
13#[cfg(feature = "gateway")]
14use crate::gateway::GatewayError;
15#[cfg(feature = "http")]
16use crate::http::HttpError;
17use crate::internal::prelude::*;
18use crate::json::JsonError;
19use crate::model::ModelError;
20
21pub type Result<T, E = Error> = StdResult<T, E>;
27
28#[derive(Debug)]
33#[non_exhaustive]
34pub enum Error {
35    Decode(&'static str, Value),
37    Format(FormatError),
39    Io(IoError),
41    #[cfg_attr(not(feature = "simd_json"), doc = "An error from the [`serde_json`] crate.")]
42    #[cfg_attr(feature = "simd_json", doc = "An error from the [`simd_json`] crate.")]
43    Json(JsonError),
44    Model(ModelError),
48    ExceededLimit(String, u32),
57    NotInRange(&'static str, u64, u64, u64),
66    Other(&'static str),
69    Url(String),
71    #[cfg(feature = "client")]
75    Client(ClientError),
76    #[cfg(feature = "gateway")]
80    Gateway(GatewayError),
81    #[cfg(feature = "http")]
85    Http(HttpError),
86    #[cfg(feature = "gateway")]
88    Tungstenite(TungsteniteError),
89}
90
91impl From<FormatError> for Error {
92    fn from(e: FormatError) -> Error {
93        Error::Format(e)
94    }
95}
96
97#[cfg(feature = "gateway")]
98impl From<GatewayError> for Error {
99    fn from(e: GatewayError) -> Error {
100        Error::Gateway(e)
101    }
102}
103
104impl From<IoError> for Error {
105    fn from(e: IoError) -> Error {
106        Error::Io(e)
107    }
108}
109
110impl From<JsonError> for Error {
111    fn from(e: JsonError) -> Error {
112        Error::Json(e)
113    }
114}
115
116impl From<ModelError> for Error {
117    fn from(e: ModelError) -> Error {
118        Error::Model(e)
119    }
120}
121
122#[cfg(feature = "gateway")]
123impl From<TungsteniteError> for Error {
124    fn from(e: TungsteniteError) -> Error {
125        Error::Tungstenite(e)
126    }
127}
128
129#[cfg(feature = "http")]
130impl From<HttpError> for Error {
131    fn from(e: HttpError) -> Error {
132        Error::Http(e)
133    }
134}
135
136#[cfg(feature = "http")]
137impl From<InvalidHeaderValue> for Error {
138    fn from(e: InvalidHeaderValue) -> Error {
139        HttpError::InvalidHeader(e).into()
140    }
141}
142
143#[cfg(feature = "http")]
144impl From<ReqwestError> for Error {
145    fn from(e: ReqwestError) -> Error {
146        HttpError::Request(e).into()
147    }
148}
149
150impl fmt::Display for Error {
151    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152        match self {
153            Self::Decode(msg, _) | Self::Other(msg) => f.write_str(msg),
154            Self::ExceededLimit(..) => f.write_str("Input exceeded a limit"),
155            Self::NotInRange(..) => f.write_str("Input is not in the specified range"),
156            Self::Format(inner) => fmt::Display::fmt(&inner, f),
157            Self::Io(inner) => fmt::Display::fmt(&inner, f),
158            Self::Json(inner) => fmt::Display::fmt(&inner, f),
159            Self::Model(inner) => fmt::Display::fmt(&inner, f),
160            Self::Url(msg) => f.write_str(msg),
161            #[cfg(feature = "client")]
162            Self::Client(inner) => fmt::Display::fmt(&inner, f),
163            #[cfg(feature = "gateway")]
164            Self::Gateway(inner) => fmt::Display::fmt(&inner, f),
165            #[cfg(feature = "http")]
166            Self::Http(inner) => fmt::Display::fmt(&inner, f),
167            #[cfg(feature = "gateway")]
168            Self::Tungstenite(inner) => fmt::Display::fmt(&inner, f),
169        }
170    }
171}
172
173impl StdError for Error {
174    #[instrument]
175    fn source(&self) -> Option<&(dyn StdError + 'static)> {
176        match self {
177            Self::Format(inner) => Some(inner),
178            Self::Io(inner) => Some(inner),
179            Self::Json(inner) => Some(inner),
180            Self::Model(inner) => Some(inner),
181            #[cfg(feature = "client")]
182            Self::Client(inner) => Some(inner),
183            #[cfg(feature = "http")]
184            Self::Http(inner) => Some(inner),
185            #[cfg(feature = "gateway")]
186            Self::Tungstenite(inner) => Some(inner),
187            _ => None,
188        }
189    }
190}