1use bigdecimal_::ParseBigDecimalError;
3use chrono::ParseError;
4use std::any::type_name;
5use std::error::Error as StdError;
6use std::fmt::{self, Debug, Display};
7use std::io;
8
9use serde::de::Visitor;
10use serde::ser::{Serialize, Serializer};
11use serde::{Deserialize, Deserializer};
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
66impl From<sqlx_core::error::BoxDynError> for crate::Error {
67 fn from(arg: BoxDynError) -> Self {
68 return crate::Error::Database(arg.to_string());
69 }
70}
71
72impl From<sqlx_core::error::Error> for crate::Error {
73 fn from(arg: sqlx_core::error::Error) -> Self {
74 return crate::Error::Database(arg.to_string());
75 }
76}
77
78impl From<chrono::ParseError> for crate::Error {
79 fn from(arg: ParseError) -> Self {
80 return crate::Error::E(arg.to_string());
81 }
82}
83
84impl From<serde_json::Error> for crate::Error {
85 fn from(arg: serde_json::Error) -> Self {
86 return crate::Error::E(arg.to_string());
87 }
88}
89
90impl From<time::ParseError> for crate::Error {
91 fn from(arg: time::ParseError) -> Self {
92 return crate::Error::E(arg.to_string());
93 }
94}
95
96impl From<rbson::ser::Error> for crate::Error {
97 fn from(arg: rbson::ser::Error) -> Self {
98 return crate::Error::E(arg.to_string());
99 }
100}
101
102impl From<rbson::de::Error> for crate::Error {
103 fn from(arg: rbson::de::Error) -> Self {
104 return crate::Error::E(arg.to_string());
105 }
106}
107
108impl Clone for Error {
109 fn clone(&self) -> Self {
110 Error::from(self.to_string())
111 }
112
113 fn clone_from(&mut self, source: &Self) {
114 *self = Self::from(source.to_string());
115 }
116}
117
118impl Serialize for Error {
120 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
121 where
122 S: Serializer,
123 {
124 serializer.serialize_str(self.to_string().as_str())
125 }
126}
127
128struct ErrorVisitor;
129
130impl<'de> Visitor<'de> for ErrorVisitor {
131 type Value = String;
132
133 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
134 formatter.write_str("a string")
135 }
136
137 fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
138 where
139 E: std::error::Error,
140 {
141 Ok(v)
142 }
143
144 fn visit_str<E>(self, v: &str) -> std::result::Result<Self::Value, E>
145 where
146 E: std::error::Error,
147 {
148 Ok(v.to_string())
149 }
150}
151
152impl<'de> Deserialize<'de> for Error {
153 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
154 where
155 D: Deserializer<'de>,
156 {
157 let r = deserializer.deserialize_string(ErrorVisitor)?;
158 return Ok(Error::from(r));
159 }
160}
161
162pub trait OptionToResult<T> {
163 fn to_result(self, error_str: &str) -> Result<T>;
164}
165
166impl<T> OptionToResult<T> for Option<T> {
167 fn to_result(self, error_str: &str) -> Result<T> {
168 if self.is_some() {
169 Ok(self.unwrap())
170 } else {
171 Err(Error::from(error_str))
172 }
173 }
174}
175
176impl From<Error> for std::io::Error {
177 fn from(arg: Error) -> Self {
178 arg.into()
179 }
180}
181
182impl From<ParseBigDecimalError> for crate::Error {
183 fn from(arg: ParseBigDecimalError) -> Self {
184 Self::E(arg.to_string())
185 }
186}
187
188impl From<uuid::Error> for crate::Error {
189 fn from(arg: uuid::Error) -> Self {
190 Self::E(arg.to_string())
191 }
192}
193
194#[test]
195fn test_json_error() {
196 let e = Error::from("fuck");
197 let s = serde_json::to_string(&e).unwrap();
198 println!("{}", s.as_str());
199}