11use hyper:: StatusCode ;
22use std:: fmt;
33use tokio:: sync:: oneshot:: error:: RecvError ;
4+ use std:: fmt:: Formatter ;
5+ use std:: borrow:: Cow ;
46
57#[ derive( Debug ) ]
68pub enum StartupError {
@@ -29,14 +31,107 @@ pub enum CommunicationError {
2931
3032#[ derive( Debug ) ]
3133pub enum ServerError {
32- Hyper ( hyper:: http:: Error ) ,
34+ HyperHttp ( hyper:: http:: Error ) ,
3335 Communication ( CommunicationError ) ,
36+ Hyper ( hyper:: Error ) ,
37+ DiscordError ( String ) ,
38+ Database ( DatabaseError ) ,
3439}
3540
3641#[ derive( Debug ) ]
3742pub enum BadRequestError {
3843 UpgradeOnly ,
3944 MissingWsKey ,
45+ NoAccessCode ,
46+ }
47+
48+ #[ derive( Debug ) ]
49+ pub enum DatabaseError {
50+ Sqlx ( sqlx:: Error ) ,
51+ Deserializing ( serde_json:: Error ) ,
52+ Serializing ( serde_json:: Error ) ,
53+ Darkredis ( darkredis:: Error ) ,
54+ }
55+
56+ #[ derive( Debug ) ]
57+ pub enum WSMessageError {
58+ Tungstenite ( tokio_tungstenite:: tungstenite:: Error ) ,
59+ Database ( DatabaseError ) ,
60+ Communication ( CommunicationError ) ,
61+ CorruptMessage ( serde_json:: Error ) ,
62+ NotAuthorized ,
63+ BadAuthorization ,
64+ AlreadyAuthorized ,
65+ ClosedGracefully ,
66+ }
67+
68+
69+ impl fmt:: Display for WSMessageError {
70+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
71+ match self {
72+ WSMessageError :: Tungstenite ( e) => write ! ( f, "Tunstenite failure: {}" , e) ,
73+ WSMessageError :: Database ( e) => write ! ( f, "Failed to fetch database information: {}" , e) ,
74+ WSMessageError :: Communication ( e) => write ! ( f, "Failed to communicate with GearBot: {}" , e) ,
75+ WSMessageError :: CorruptMessage ( e) => write ! ( f, "Corrupt message recieved: {}" , e) ,
76+ WSMessageError :: NotAuthorized => write ! ( f, "Someone didn't identify themselves" ) ,
77+ WSMessageError :: BadAuthorization => write ! ( f, "Someone gave us an invalid token to try and identify" ) ,
78+ WSMessageError :: AlreadyAuthorized => write ! ( f, "Someone double identified" ) ,
79+ WSMessageError :: ClosedGracefully => write ! ( f, "Connection closed by client" )
80+ }
81+ }
82+ }
83+
84+ const CORRUPT_MESSAGE : & str = "Corrupt message recieved" ;
85+ const NOT_AUTHORIZED : & str = "You failed to identify yourself first, access denied!" ;
86+ const BAD_AUTHORIZATION : & str = "You failed to identify yourself first, access denied!" ;
87+ const ALREADY_AUTHORIZED : & str = "You can not identify twice!" ;
88+ const TUNGSTENITE : & str = "Unable to process message" ;
89+
90+ impl WSMessageError {
91+ pub fn closes_socket ( & self ) -> bool {
92+ match self {
93+ WSMessageError :: Tungstenite ( _) |
94+ WSMessageError :: CorruptMessage ( _) |
95+ WSMessageError :: NotAuthorized |
96+ WSMessageError :: AlreadyAuthorized |
97+ WSMessageError :: BadAuthorization => true ,
98+ _ => false
99+ }
100+ }
101+
102+ pub fn get_close_message ( & self ) -> & ' static str {
103+ match self {
104+ WSMessageError :: CorruptMessage ( _) => CORRUPT_MESSAGE ,
105+ WSMessageError :: NotAuthorized => NOT_AUTHORIZED ,
106+ WSMessageError :: BadAuthorization => BAD_AUTHORIZATION ,
107+ WSMessageError :: AlreadyAuthorized => ALREADY_AUTHORIZED ,
108+ WSMessageError :: Tungstenite ( _) => TUNGSTENITE ,
109+ _ => unreachable ! ( )
110+ }
111+ }
112+ }
113+
114+ impl fmt:: Display for DatabaseError {
115+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
116+ match self {
117+ DatabaseError :: Sqlx ( e) => write ! ( f, "Database failure: {:?}" , e) ,
118+ DatabaseError :: Deserializing ( e) => write ! ( f, "Failed to deserialize: {}" , e) ,
119+ DatabaseError :: Serializing ( e) => write ! ( f, "Failed to seralize: {}" , e) ,
120+ DatabaseError :: Darkredis ( e) => write ! ( f, "Redis failure: {}" , e) ,
121+ }
122+ }
123+ }
124+
125+ impl From < darkredis:: Error > for DatabaseError {
126+ fn from ( e : darkredis:: Error ) -> Self {
127+ DatabaseError :: Darkredis ( e)
128+ }
129+ }
130+
131+ impl From < sqlx:: Error > for DatabaseError {
132+ fn from ( e : sqlx:: Error ) -> Self {
133+ DatabaseError :: Sqlx ( e)
134+ }
40135}
41136
42137impl RequestError {
@@ -75,8 +170,11 @@ impl fmt::Display for StartupError {
75170impl fmt:: Display for ServerError {
76171 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
77172 match self {
78- ServerError :: Hyper ( e) => write ! ( f, "Error assembling hyper response: {}" , e) ,
173+ ServerError :: HyperHttp ( e) => write ! ( f, "Error assembling hyper response: {}" , e) ,
79174 ServerError :: Communication ( e) => write ! ( f, "Error communicating with GearBot: {}" , e) ,
175+ ServerError :: Hyper ( e) => write ! ( f, "Error making a request to the discord api: {}" , e) ,
176+ ServerError :: Database ( e) => write ! ( f, "Database error occured: {}" , e) ,
177+ ServerError :: DiscordError ( e) => write ! ( f, "Error making a request to discord: {}" , e)
80178 }
81179 }
82180}
@@ -107,7 +205,7 @@ impl fmt::Display for CommunicationError {
107205
108206impl From < hyper:: http:: Error > for RequestError {
109207 fn from ( e : hyper:: http:: Error ) -> Self {
110- RequestError :: Server ( ServerError :: Hyper ( e) )
208+ RequestError :: Server ( ServerError :: HyperHttp ( e) )
111209 }
112210}
113211
@@ -140,3 +238,27 @@ impl From<BadRequestError> for RequestError {
140238 RequestError :: BadRequest ( e)
141239 }
142240}
241+
242+ impl From < hyper:: Error > for RequestError {
243+ fn from ( e : hyper:: Error ) -> Self {
244+ RequestError :: Server ( ServerError :: Hyper ( e) )
245+ }
246+ }
247+
248+ impl From < DatabaseError > for RequestError {
249+ fn from ( e : DatabaseError ) -> Self {
250+ RequestError :: Server ( ServerError :: Database ( e) )
251+ }
252+ }
253+
254+ impl From < DatabaseError > for WSMessageError {
255+ fn from ( e : DatabaseError ) -> Self {
256+ WSMessageError :: Database ( e)
257+ }
258+ }
259+
260+ impl From < CommunicationError > for WSMessageError {
261+ fn from ( e : CommunicationError ) -> Self {
262+ WSMessageError :: Communication ( e)
263+ }
264+ }
0 commit comments