1use std::any::type_name;
3use std::error::Error as StdError;
4use std::fmt::{self, Debug, Display};
5use std::io;
6use bigdecimal_::ParseBigDecimalError;
7use chrono::ParseError;
8
9use serde::{Deserialize, Deserializer};
10use serde::de::Visitor;
11use serde::ser::{Serialize, Serializer};
12use sqlx_core::error::BoxDynError;
13
14pub type Result<T> = std::result::Result<T, Error>;
16
17#[derive(Debug)]
19#[non_exhaustive]
20pub enum Error {
21 E(String),
23 Deserialize(String),
24 Database(String),
25}
26
27impl Display for Error {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Error::E(error) => write!(f, "{}", error),
33 Error::Deserialize(error) => write!(f, "{}", error),
34 Error::Database(error) => write!(f, "{}", error.to_string()),
35 }
36 }
37}
38
39impl StdError for Error {}
40
41impl From<io::Error> for Error {
42 #[inline]
43 fn from(err: io::Error) -> Self {
44 Error::from(err.to_string())
45 }
46}
47
48impl From<&str> for Error {
49 fn from(arg: &str) -> Self {
50 return Error::E(arg.to_string());
51 }
52}
53
54impl From<std::string::String> for Error {
55 fn from(arg: String) -> Self {
56 return Error::E(arg);
57 }
58}
59
60impl From<&dyn std::error::Error> for Error {
61 fn from(arg: &dyn std::error::Error) -> Self {
62 return Error::E(arg.to_string());
63 }
64}
65
66
67impl From<sqlx_core::error::BoxDynError> for crate::Error {
68 fn from(arg: BoxDynError) -> Self {
69 return crate::Error::Database(arg.to_string());
70 }
71}
72
73impl From<sqlx_core::error::Error> for crate::Error {
74 fn from(arg: sqlx_core::error::Error) -> Self {
75 return crate::Error::Database(arg.to_string());
76 }
77}
78
79impl From<chrono::ParseError> for crate::Error {
80 fn from(arg: ParseError) -> Self {
81 return crate::Error::E(arg.to_string());
82 }
83}
84
85impl From<serde_json::Error> for crate::Error {
86 fn from(arg: serde_json::Error) -> Self {
87 return crate::Error::E(arg.to_string());
88 }
89}
90
91impl From<time::ParseError> for crate::Error {
92 fn from(arg: time::ParseError) -> Self {
93 return crate::Error::E(arg.to_string());
94 }
95}
96
97impl From<rbson::ser::Error> for crate::Error {
98 fn from(arg: rbson::ser::Error) -> Self {
99 return crate::Error::E(arg.to_string());
100 }
101}
102
103impl From<rbson::de::Error> for crate::Error {
104 fn from(arg: rbson::de::Error) -> Self {
105 return crate::Error::E(arg.to_string());
106 }
107}
108
109impl Clone for Error {
110 fn clone(&self) -> Self {
111 Error::from(self.to_string())
112 }
113
114 fn clone_from(&mut self, source: &Self) {
115 *self = Self::from(source.to_string());
116 }
117}
118
119impl Serialize for Error {
121 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
122 where
123 S: Serializer,
124 {
125 serializer.serialize_str(self.to_string().as_str())
126 }
127}
128
129struct ErrorVisitor;
130
131impl<'de> Visitor<'de> for ErrorVisitor {
132 type Value = String;
133
134 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
135 formatter.write_str("a string")
136 }
137
138 fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
139 where
140 E: std::error::Error,
141 {
142 Ok(v)
143 }
144
145 fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
146 where
147 E: std::error::Error,
148 {
149 Ok(v.to_string())
150 }
151}
152
153impl<'de> Deserialize<'de> for Error {
154 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
155 where
156 D: Deserializer<'de>,
157 {
158 let r = deserializer.deserialize_string(ErrorVisitor)?;
159 return Ok(Error::from(r));
160 }
161}
162
163pub trait OptionToResult<T> {
164 fn to_result(self, error_str: &str) -> Result<T>;
165}
166
167impl<T> OptionToResult<T> for Option<T> {
168 fn to_result(self, error_str: &str) -> Result<T> {
169 if self.is_some() {
170 Ok(self.unwrap())
171 } else {
172 Err(Error::from(error_str))
173 }
174 }
175}
176
177
178impl From<Error> for std::io::Error {
179 fn from(arg: Error) -> Self {
180 arg.into()
181 }
182}
183
184impl From<ParseBigDecimalError> for crate::Error {
185 fn from(arg: ParseBigDecimalError) -> Self {
186 Self::E(arg.to_string())
187 }
188}
189
190impl From<uuid::Error> for crate::Error {
191 fn from(arg: uuid::Error) -> Self {
192 Self::E(arg.to_string())
193 }
194}
195
196#[test]
197fn test_json_error() {
198 let e = Error::from("fuck");
199 let s = serde_json::to_string(&e).unwrap();
200 println!("{}", s.as_str());
201}