spectacles_gateway/
errors.rs1use std::{
2 error::Error as StdError,
3 fmt::{Display, Formatter, Result as FmtResult},
4 io::Error as IoError,
5 result::Result as StdResult,
6};
7
8use futures::sync::mpsc::SendError;
9use reqwest::Error as ReqwestError;
10use serde_json::Error as JsonError;
11use tokio::timer::Error as TimerError;
12use tokio_tungstenite::tungstenite::{
13 Error as TungsteniteError,
14 Message as TungsteniteMessage
15};
16
17pub type Result<T> = StdResult<T, Error>;
19
20#[derive(Debug)]
22pub enum Error {
23 Tungstenite(TungsteniteError),
24 Json(JsonError),
25 Reqwest(ReqwestError),
26 Timer(TimerError),
27 InvalidTokenError,
28 Io(IoError),
29 TungsteniteSend(SendError<TungsteniteMessage>)
30}
31
32impl Display for Error {
33 fn fmt(&self, f: &mut Formatter) -> FmtResult {
34 f.write_str(self.description())
35 }
36}
37
38impl StdError for Error {
39 fn description(&self) -> &str {
40 match self {
41 Error::Reqwest(e) => e.description(),
42 Error::Timer(e) => e.description(),
43 Error::Tungstenite(e) => e.description(),
44 Error::Io(e) => e.description(),
45 Error::TungsteniteSend(e) => e.description(),
46 Error::Json(e) => e.description(),
47 Error::InvalidTokenError =>
48 "The token provided was not accepted by Discord. Please check that your token is correct and try again."
49 }
50 }
51}
52
53impl From<TungsteniteError> for Error {
54 fn from(err: TungsteniteError) -> Self {
55 Error::Tungstenite(err)
56 }
57}
58
59impl From<IoError> for Error {
60 fn from(err: IoError) -> Self {
61 Error::Io(err)
62 }
63}
64
65impl From<TimerError> for Error {
66 fn from(err: TimerError) -> Self {
67 Error::Timer(err)
68 }
69}
70impl From<ReqwestError> for Error {
71 fn from(err: ReqwestError) -> Self {
72 if let Some(t) = err.status() {
73 match t.as_u16() {
74 401 => Error::InvalidTokenError,
75 _ => Error::Reqwest(err)
76 }
77 } else {
78 Error::Reqwest(err)
79 }
80 }
81}
82
83impl From<SendError<TungsteniteMessage>> for Error {
84 fn from(err: SendError<TungsteniteMessage>) -> Self {
85 Error::TungsteniteSend(err)
86 }
87}
88
89impl From<JsonError> for Error {
90 fn from(err: JsonError) -> Self {
91 Error::Json(err)
92 }
93}