1use thiserror::Error;
4
5#[derive(Error, Debug, Clone)]
7pub enum QueryError {
8 #[error("Network error: {0}")]
9 Network(String),
10
11 #[error("HTTP error {status}: {message}")]
12 Http { status: u16, message: String },
13
14 #[error("Unauthorized")]
15 Unauthorized,
16
17 #[error("Not found: {0}")]
18 NotFound(String),
19
20 #[error("Validation error: {0}")]
21 Validation(String),
22
23 #[error("Timeout after {0:?}")]
24 Timeout(std::time::Duration),
25
26 #[error("{0}")]
27 Custom(String),
28}
29
30impl QueryError {
31 pub fn is_retryable(&self) -> bool {
33 matches!(
34 self,
35 QueryError::Network(_)
36 | QueryError::Timeout(_)
37 | QueryError::Http {
38 status: 500..=599,
39 ..
40 }
41 )
42 }
43
44 pub fn network(msg: impl Into<String>) -> Self {
46 Self::Network(msg.into())
47 }
48
49 pub fn custom(msg: impl Into<String>) -> Self {
51 Self::Custom(msg.into())
52 }
53}
54
55impl From<String> for QueryError {
56 fn from(s: String) -> Self {
57 Self::Custom(s)
58 }
59}
60
61impl From<&str> for QueryError {
62 fn from(s: &str) -> Self {
63 Self::Custom(s.to_string())
64 }
65}