1use serde::Deserialize;
2use std::fmt::{Debug, Display};
3use std::result;
4use std::sync::PoisonError;
5
6pub type Result<T> = result::Result<T, Error>;
8
9#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
10pub enum Error {
12 IOError(String),
13 InvalidResponse(String),
14 ParseError(String),
15 InternalError(String),
16 RuntimeError(String),
17 ServerDisconnect(String),
18 ConnectionError(String),
19 Timeout,
20 Forbidden,
21 TooManyRequests,
22 BadRequest,
23}
24
25impl From<tokio_websockets::Error> for Error {
26 fn from(err: tokio_websockets::Error) -> Error {
27 Error::ConnectionError(err.to_string())
28 }
29}
30
31impl From<url::ParseError> for Error {
32 fn from(e: url::ParseError) -> Self {
33 Error::ParseError(e.to_string())
34 }
35}
36
37impl<T: Debug> From<PoisonError<T>> for Error {
38 fn from(e: PoisonError<T>) -> Self {
39 Error::InternalError(e.to_string())
40 }
41}
42
43impl From<serde_json::Error> for Error {
44 fn from(e: serde_json::Error) -> Self {
45 Error::ParseError(e.to_string())
46 }
47}
48
49impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
50 fn from(e: tokio::sync::mpsc::error::SendError<T>) -> Self {
51 Error::InternalError(e.to_string())
52 }
53}
54
55impl From<&str> for Error {
56 fn from(s: &str) -> Self {
57 Error::InternalError(s.to_string())
58 }
59}
60
61impl From<String> for Error {
62 fn from(s: String) -> Self {
63 Error::InternalError(s)
64 }
65}
66
67impl From<std::io::Error> for Error {
68 fn from(e: std::io::Error) -> Self {
69 Error::IOError(e.to_string())
70 }
71}
72
73impl Display for Error {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self {
76 Self::IOError(s) => write!(f, "IO error: {s}"),
77 Self::InvalidResponse(s) => write!(f, "Invalid response from server: {s}"),
78 Self::ParseError(s) => write!(f, "Failed to parse response from server: {s}"),
79 Self::InternalError(s) => write!(f, "Internal error: {s}"),
80 Self::RuntimeError(s) => write!(f, "Runtime error: {s}"),
81 Self::ServerDisconnect(s) => write!(f, "Disconnected from server: {s}"),
82 Self::ConnectionError(s) => write!(f, "Server connection closed due to error: {s}"),
83 Self::Timeout => write!(f, "Timed out waiting for server message"),
84 Self::Forbidden => f.write_str("Invalid credentials"),
85 Self::TooManyRequests => f.write_str("Rate limited"),
86 Self::BadRequest => f.write_str("Malformed request"),
87 }
88 }
89}
90
91impl std::error::Error for Error {}