Skip to content

Commit 8d400a1

Browse files
breaking change to how errors work, implemented std::error::Error for influxdb_rs::Error
1 parent 0407d75 commit 8d400a1

File tree

5 files changed

+120
-48
lines changed

5 files changed

+120
-48
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "influxdb_rs"
3-
version = "0.1.5"
3+
version = "0.2.0"
44
authors = ["Harry Thomas @infosechoudini"]
55
description = "InfluxDBv2 Rust driver"
66
readme = "README.md"

src/api/query.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,27 @@ impl Client {
3232
400 => {
3333
let json_data = res.text().await?;
3434

35-
return Err(error::Error::SyntaxError(serialization::conversion(
35+
return Err(error::Error{
36+
inner: error::ErrorKind::SyntaxError(serialization::conversion(
3637
&json_data,
37-
)));
38+
))});
3839
}
3940
401 | 403 => {
40-
return Err(error::Error::InvalidCredentials(
41+
return Err(error::Error{
42+
inner: error::ErrorKind::InvalidCredentials(
4143
"Invalid authentication credentials.".to_string()
42-
));
44+
)});
4345
}
4446
_ => {
4547
let err = res.text().await?;
46-
return Err(error::Error::Unknown(err));
48+
return Err(error::Error{
49+
inner: error::ErrorKind::Unknown(err)});
4750
}
4851
}
4952

5053
} else {
51-
return Err(error::Error::Unknown("No flux query to serialize".to_string()));
54+
return Err(error::Error{
55+
inner: error::ErrorKind::Unknown("No flux query to serialize".to_string())});
5256
};
5357

5458

src/client.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ impl Client {
8282
_ => {
8383
let err = res.text().await?;
8484

85-
Err(error::Error::SyntaxError(serialization::conversion(&err)))
85+
Err(error::Error{
86+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))
87+
})
8688
}
8789
}
8890

@@ -153,7 +155,9 @@ impl Client {
153155
let err = res.text().await?;
154156
match status{
155157
204 => Ok(true),
156-
_ => Err(error::Error::SyntaxError(serialization::conversion(&err))),
158+
_ => Err(error::Error{
159+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))
160+
}),
157161
}
158162
}
159163

@@ -186,7 +190,9 @@ impl Client {
186190
_ => {
187191
let err = res.text().await?;
188192

189-
Err(error::Error::SyntaxError(serialization::conversion(&err)))
193+
Err(error::Error{
194+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))
195+
})
190196
}
191197
}
192198

@@ -232,18 +238,24 @@ impl Client {
232238

233239
match status {
234240
204 => Ok(()),
235-
400 => Err(error::Error::SyntaxError(serialization::conversion(&err))),
236-
401 | 403 => Err(error::Error::InvalidCredentials(
241+
400 => Err(error::Error {
242+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))
243+
}),
244+
401 | 403 => Err(error::Error {
245+
inner: error::ErrorKind::InvalidCredentials(
237246
"Invalid authentication credentials.".to_string(),
238-
)),
239-
404 => Err(error::Error::DataBaseDoesNotExist(
247+
)}),
248+
404 => Err(error::Error{
249+
inner: error::ErrorKind::DataBaseDoesNotExist(
240250
serialization::conversion(&err),
241-
)),
242-
500 => Err(error::Error::RetentionPolicyDoesNotExist(err)),
243-
status => Err(error::Error::Unknown(format!(
251+
)}),
252+
500 => Err(error::Error{
253+
inner: error::ErrorKind::RetentionPolicyDoesNotExist(err)}),
254+
status => Err(error::Error{
255+
inner: error::ErrorKind::Unknown(format!(
244256
"Received status code {}",
245257
status
246-
))),
258+
))}),
247259
}
248260
}
249261

@@ -277,14 +289,18 @@ impl Client {
277289
400 => {
278290
let err = res.text().await?;
279291

280-
Err(error::Error::SyntaxError(serialization::conversion(
292+
Err(error::Error{
293+
inner: error::ErrorKind::SyntaxError(serialization::conversion(
281294
&err,
282-
)))
295+
))})
283296
}
284-
401 | 403 => Err(error::Error::InvalidCredentials(
297+
401 | 403 => Err(error::Error{
298+
inner: error::ErrorKind::InvalidCredentials(
285299
"Invalid authentication credentials.".to_string(),
286-
)),
287-
_ => Err(error::Error::Unknown("There is something wrong".to_string())),
300+
)}),
301+
_ => Err(error::Error{
302+
inner: error::ErrorKind::Unknown("There is something wrong".to_string())
303+
}),
288304
}
289305
}
290306

@@ -318,8 +334,11 @@ impl Client {
318334

319335
match status {
320336
201 => Ok(()),
321-
422 => Err(error::Error::SyntaxError(serialization::conversion(&err))),
322-
_ => Err(error::Error::SyntaxError(serialization::conversion(&err)))
337+
422 => Err(error::Error{
338+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))}),
339+
_ => Err(error::Error{
340+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))
341+
})
323342
}
324343
}
325344

@@ -345,7 +364,8 @@ impl Client {
345364
_ => {
346365
let err = res.text().await?;
347366

348-
Err(error::Error::SyntaxError(serialization::conversion(&err)))
367+
Err(error::Error{
368+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))})
349369
}
350370
}
351371

@@ -370,8 +390,10 @@ impl Client {
370390

371391
match status {
372392
204 => Ok(()),
373-
404 => Err(error::Error::SyntaxError(serialization::conversion(&err))),
374-
_ => Err(error::Error::SyntaxError(serialization::conversion(&err)))
393+
404 => Err(error::Error{
394+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))}),
395+
_ => Err(error::Error{
396+
inner: error::ErrorKind::SyntaxError(serialization::conversion(&err))})
375397
}
376398
}
377399

src/error.rs

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@ use futures::Future;
55
use std::pin::Pin;
66
use futures::task::{Poll, Context};
77

8+
9+
/// Influxdb-rs Error
10+
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
11+
pub struct Error {
12+
/// Holds the inner error kind
13+
pub inner: ErrorKind,
14+
}
15+
16+
impl fmt::Display for Error {
17+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18+
write!(f, "{}", self.inner)
19+
}
20+
}
21+
22+
823
/// The error of influxdb client
924
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
10-
pub enum Error {
11-
/// Syntax error, some is bug, some is SQL error. If it's a bug, welcome to PR.
25+
pub enum ErrorKind {
26+
/// Syntax error
1227
SyntaxError(String),
1328
/// Invalid credentials
1429
InvalidCredentials(String),
@@ -22,28 +37,36 @@ pub enum Error {
2237
Unknown(String),
2338
}
2439

25-
impl fmt::Display for Error {
40+
impl fmt::Display for ErrorKind {
2641
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
match *self {
28-
Error::SyntaxError(ref t) => write!(f, "{}", t),
29-
Error::InvalidCredentials(ref t) => write!(f, "{}", t),
30-
Error::DataBaseDoesNotExist(ref t) => write!(f, "{}", t),
31-
Error::RetentionPolicyDoesNotExist(ref t) => write!(f, "{}", t),
32-
Error::Communication(ref t) => write!(f, "{}", t),
33-
Error::Unknown(ref t) => write!(f, "{}", t),
42+
match self {
43+
ErrorKind::SyntaxError(ref t) => write!(f, "{}", t),
44+
ErrorKind::InvalidCredentials(ref t) => write!(f, "{}", t),
45+
ErrorKind::DataBaseDoesNotExist(ref t) => write!(f, "{}", t),
46+
ErrorKind::RetentionPolicyDoesNotExist(ref t) => write!(f, "{}", t),
47+
ErrorKind::Communication(ref t) => write!(f, "{}", t),
48+
ErrorKind::Unknown(ref t) => write!(f, "{}", t),
3449
}
3550
}
3651
}
3752

53+
impl std::error::Error for Error {}
54+
55+
impl std::error::Error for ErrorKind {}
56+
3857
impl From<io::Error> for Error {
3958
fn from(err: io::Error) -> Self {
40-
Error::Communication(format!("{}", err))
59+
Error{
60+
inner: ErrorKind::Communication(format!("{}", err)),
61+
}
4162
}
4263
}
4364

4465
impl From<reqwest::Error> for Error {
4566
fn from(err: reqwest::Error) -> Self {
46-
Error::Communication(format!("{}", err))
67+
Error {
68+
inner: ErrorKind::Communication(format!("{}", err))
69+
}
4770
}
4871
}
4972

@@ -55,14 +78,38 @@ impl Future for Error {
5578
// method we'll need to call to drive futures to completion.
5679
fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
5780

58-
match *self {
59-
Error::SyntaxError(ref t) => Poll::Ready(format!("{}", t).to_string()),
60-
Error::InvalidCredentials(ref t) => Poll::Ready(format!("{}", t).to_string()),
61-
Error::DataBaseDoesNotExist(ref t) => Poll::Ready(format!("{}", t).to_string()),
62-
Error::RetentionPolicyDoesNotExist(ref t) => Poll::Ready(format!("{}", t).to_string()),
63-
Error::Communication(ref t) => Poll::Ready(format!("{}", t).to_string()),
64-
Error::Unknown(ref t) => Poll::Ready(format!("{}", t).to_string()),
81+
match self.inner {
82+
ErrorKind::SyntaxError(ref t) => Poll::Ready(format!("{}", t).to_string()),
83+
ErrorKind::InvalidCredentials(ref t) => Poll::Ready(format!("{}", t).to_string()),
84+
ErrorKind::DataBaseDoesNotExist(ref t) => Poll::Ready(format!("{}", t).to_string()),
85+
ErrorKind::RetentionPolicyDoesNotExist(ref t) => Poll::Ready(format!("{}", t).to_string()),
86+
ErrorKind::Communication(ref t) => Poll::Ready(format!("{}", t).to_string()),
87+
ErrorKind::Unknown(ref t) => Poll::Ready(format!("{}", t).to_string()),
6588
}
6689
}
6790

91+
}
92+
93+
#[cfg(test)]
94+
mod tests {
95+
96+
use crate::serialization;
97+
use crate::{Error, error::ErrorKind};
98+
99+
#[test]
100+
fn syntax_error() {
101+
let syntax = Error {
102+
inner: ErrorKind::SyntaxError(serialization::conversion(
103+
"BAD SYNTAX",)),
104+
};
105+
106+
match syntax.inner {
107+
ErrorKind::SyntaxError(ref e) => {
108+
assert_eq!(e.as_str(), "BAD SYNTAX")
109+
},
110+
_ => panic!("WAS NOT SYNTAX ERROR"),
111+
}
112+
113+
114+
}
68115
}

src/rocket_driver/influx_config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub struct Config {
1212
pub url: String,
1313
/// Minimum number of connections to maintain in the pool.
1414
///
15-
/// **Note:** `deadpool` drivers do not support and thus ignore this value.
1615
///
1716
/// _Default:_ `None`.
1817
pub min_connections: Option<u32>,

0 commit comments

Comments
 (0)