1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use log::SetLoggerError;
use std::{
	any::Any,
	backtrace::Backtrace,
	collections::HashMap,
	fmt,
	fmt::{Debug, Formatter},
	num::ParseIntError,
};
use tokio::{sync::mpsc::error::SendError, time::error::Elapsed};

#[derive(thiserror::Error, Debug)]
pub struct QuerentError {
	pub message: String,
	pub cause: QuerentErrorCauseType,
	pub backtrace: Option<Backtrace>,
}

#[derive(Debug, Clone)]
pub enum QuerentErrorCauseType {
	User(Option<HashMap<String, String>>),
	Internal(Option<HashMap<String, String>>),
}

impl QuerentError {
	pub fn user(message: String) -> Self {
		Self {
			message,
			cause: QuerentErrorCauseType::User(None),
			backtrace: Some(Backtrace::capture()),
		}
	}

	pub fn internal(message: String) -> Self {
		Self {
			message,
			cause: QuerentErrorCauseType::Internal(None),
			backtrace: Some(Backtrace::capture()),
		}
	}

	pub fn internal_with_bt(message: String, backtrace: Option<Backtrace>) -> Self {
		Self { message, cause: QuerentErrorCauseType::Internal(None), backtrace }
	}

	pub fn panic(error: Box<dyn Any + Send>) -> Self {
		if let Some(reason) = error.downcast_ref::<&str>() {
			QuerentError::internal(format!("Unexpected panic. Reason: {}", reason))
		} else if let Some(reason) = error.downcast_ref::<String>() {
			QuerentError::internal(format!("Unexpected panic. Reason: {}", reason))
		} else {
			QuerentError::internal("Unexpected panic without reason".to_string())
		}
	}
}

impl QuerentError {
	pub fn backtrace(&self) -> Option<&Backtrace> {
		self.backtrace.as_ref()
	}

	pub fn to_backtrace(self) -> Option<Backtrace> {
		self.backtrace
	}
}

impl fmt::Display for QuerentError {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		match self.cause {
			QuerentErrorCauseType::User(_) => f.write_fmt(format_args!("User: {}", self.message)),
			QuerentErrorCauseType::Internal(_) =>
				f.write_fmt(format_args!("Internal: {}", self.message)),
		}
	}
}

impl From<std::io::Error> for QuerentError {
	fn from(v: std::io::Error) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::task::JoinError> for QuerentError {
	fn from(v: tokio::task::JoinError) -> Self {
		if v.is_panic() {
			QuerentError::panic(v.into_panic())
		} else {
			// JoinError can return CanceledError
			QuerentError::internal(v.to_string())
		}
	}
}

impl<T> From<SendError<T>> for QuerentError
where
	T: Debug,
{
	fn from(v: SendError<T>) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<std::time::SystemTimeError> for QuerentError {
	fn from(v: std::time::SystemTimeError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<Elapsed> for QuerentError {
	fn from(v: Elapsed) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::sync::broadcast::error::RecvError> for QuerentError {
	fn from(v: tokio::sync::broadcast::error::RecvError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<std::string::FromUtf8Error> for QuerentError {
	fn from(v: std::string::FromUtf8Error) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::sync::oneshot::error::RecvError> for QuerentError {
	fn from(v: tokio::sync::oneshot::error::RecvError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::sync::watch::error::SendError<bool>> for QuerentError {
	fn from(v: tokio::sync::watch::error::SendError<bool>) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::sync::watch::error::RecvError> for QuerentError {
	fn from(v: tokio::sync::watch::error::RecvError) -> Self {
		QuerentError::internal(v.to_string())
	}
}
impl From<ParseIntError> for QuerentError {
	fn from(v: ParseIntError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<SetLoggerError> for QuerentError {
	fn from(v: SetLoggerError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<serde_json::Error> for QuerentError {
	fn from(v: serde_json::Error) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<std::num::ParseFloatError> for QuerentError {
	fn from(v: std::num::ParseFloatError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<base64::DecodeError> for QuerentError {
	fn from(v: base64::DecodeError) -> Self {
		QuerentError::internal(v.to_string())
	}
}

impl From<tokio::sync::AcquireError> for QuerentError {
	fn from(v: tokio::sync::AcquireError) -> Self {
		QuerentError::internal(v.to_string())
	}
}