-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy patherrors.rs
194 lines (163 loc) · 5.63 KB
/
errors.rs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::DeError;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("an IO error occurred: {detail}")]
IOError {
#[from]
detail: std::io::Error,
},
#[error("Invalid URI: {0}")]
UrlParseError(#[from] url::ParseError),
#[error("Unsupported URI scheme: {0}")]
UnsupportedScheme(String),
#[error("Invalid DNS name: {0}")]
InvalidDnsName(String),
#[error("connection error")]
ConnectionError,
#[error("attempted to serialize excessively long string")]
StringTooLong,
#[error("attempted to serialize excessively large map")]
MapTooBig,
#[error("attempted to serialize excessively large byte array")]
BytesTooBig,
#[error("attempted to serialize excessively long list")]
ListTooLong,
#[error("invalid config")]
InvalidConfig,
#[error("{0}")]
UnsupportedVersion(String),
#[error("{0}")]
UnexpectedMessage(String),
#[error("{0}")]
UnknownType(String),
#[error("{0}")]
UnknownMessage(String),
#[error("conversion error")]
ConversionError,
#[error("{0}")]
AuthenticationError(String),
#[error("{0}")]
InvalidTypeMarker(String),
#[error("{0}")]
DeserializationError(DeError),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Neo4jErrorKind {
Client(Neo4jClientErrorKind),
Transient,
Database,
Unknown,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Neo4jClientErrorKind {
Security(Neo4jSecurityErrorKind),
SessionExpired,
FatalDiscovery,
TransactionTerminated,
ProtocolViolation,
Other,
Unknown,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Neo4jSecurityErrorKind {
Authentication,
AuthorizationExpired,
TokenExpired,
Other,
Unknown,
}
impl Neo4jErrorKind {
pub(crate) fn new(code: &str) -> Self {
let code = Self::adjust_code(code).unwrap_or(code);
Self::classify(code)
}
fn adjust_code(code: &str) -> Option<&str> {
match code {
"Neo.TransientError.Transaction.LockClientStopped" => {
Some("Neo.ClientError.Transaction.LockClientStopped")
}
"Neo.TransientError.Transaction.Terminated" => {
Some("Neo.ClientError.Transaction.Terminated")
}
_ => None,
}
}
fn classify(code: &str) -> Self {
let mut parts = code.split('.').skip(1);
let [class, subclass, kind] = [parts.next(), parts.next(), parts.next()];
match class {
Some("ClientError") => match (subclass, kind) {
(Some("Security"), Some("Unauthorized")) => Self::Client(
Neo4jClientErrorKind::Security(Neo4jSecurityErrorKind::Authentication),
),
(Some("Security"), Some("AuthorizationExpired")) => Self::Client(
Neo4jClientErrorKind::Security(Neo4jSecurityErrorKind::AuthorizationExpired),
),
(Some("Security"), Some("TokenExpired")) => Self::Client(
Neo4jClientErrorKind::Security(Neo4jSecurityErrorKind::TokenExpired),
),
(Some("Database"), Some("DatabaseNotFound")) => {
Self::Client(Neo4jClientErrorKind::FatalDiscovery)
}
(Some("Transaction"), Some("Terminated")) => {
Self::Client(Neo4jClientErrorKind::TransactionTerminated)
}
(Some("Security"), Some(_)) => Self::Client(Neo4jClientErrorKind::Security(
Neo4jSecurityErrorKind::Other,
)),
(Some("Security"), _) => Self::Client(Neo4jClientErrorKind::Security(
Neo4jSecurityErrorKind::Unknown,
)),
(Some("Request"), _) => Self::Client(Neo4jClientErrorKind::ProtocolViolation),
(Some("Cluster"), Some("NotALeader")) => {
Self::Client(Neo4jClientErrorKind::SessionExpired)
}
(Some("General"), Some("ForbiddenOnReadOnlyDatabase")) => {
Self::Client(Neo4jClientErrorKind::SessionExpired)
}
(Some(_), _) => Self::Client(Neo4jClientErrorKind::Other),
_ => Self::Client(Neo4jClientErrorKind::Unknown),
},
Some("TransientError") => Self::Transient,
Some(_) => Self::Database,
None => Self::Unknown,
}
}
pub(crate) fn can_retry(&self) -> bool {
matches!(
self,
Self::Client(
Neo4jClientErrorKind::Security(Neo4jSecurityErrorKind::AuthorizationExpired)
| Neo4jClientErrorKind::SessionExpired
) | Self::Transient
)
}
}
impl From<&str> for Neo4jErrorKind {
fn from(code: &str) -> Self {
Self::new(code)
}
}
impl std::convert::From<deadpool::managed::PoolError<Error>> for Error {
fn from(e: deadpool::managed::PoolError<Error>) -> Self {
match e {
deadpool::managed::PoolError::Backend(e) => e,
_ => Error::ConnectionError,
}
}
}
impl std::convert::From<deadpool::managed::BuildError<Error>> for Error {
fn from(value: deadpool::managed::BuildError<Error>) -> Self {
match value {
deadpool::managed::BuildError::Backend(e) => e,
_ => Error::ConnectionError,
}
}
}
pub fn unexpected<T: std::fmt::Debug>(response: T, request: &str) -> Error {
Error::UnexpectedMessage(format!(
"unexpected response for {}: {:?}",
request, response
))
}