Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Errors #150

Merged
merged 11 commits into from
Apr 4, 2024
Merged
12 changes: 4 additions & 8 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//! ```

use futures_util::TryFutureExt;
use http::StatusCode;
#[cfg(feature = "reqwest")]
use reqwest::{Client as HttpClient, RequestBuilder, Response as HttpResponse};
use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -281,7 +280,7 @@ impl Client {
})?;

// todo: improve error parsing without serde
if s.contains("\"error\"") {
if s.contains("\"error\"") || s.contains("\"Error\"") {
return Err(Error::DatabaseError {
error: format!("influxdb error: \"{}\"", s),
});
Expand All @@ -301,13 +300,10 @@ impl Client {

pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {
let status = res.status();
if status == StatusCode::UNAUTHORIZED.as_u16() {
Err(Error::AuthorizationError)
} else if status == StatusCode::FORBIDDEN.as_u16() {
Err(Error::AuthenticationError)
} else {
Ok(())
if !status.is_success() {
return Err(Error::ApiError(status.into()));
}
Ok(())
}

#[cfg(test)]
Expand Down
10 changes: 3 additions & 7 deletions influxdb/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ pub enum Error {
/// Error which has happened inside InfluxDB
DatabaseError { error: String },

#[error("authentication error. No or incorrect credentials")]
/// Error happens when no or incorrect credentials are used. `HTTP 401 Unauthorized`
AuthenticationError,

#[error("authorization error. User not authorized")]
/// Error happens when the supplied user is not authorized. `HTTP 403 Forbidden`
AuthorizationError,
#[error("API error with a status code: {0}")]
/// Error happens when API returns non 2xx status code.
ApiError(u16),

#[error("connection error: {error}")]
/// Error happens when HTTP request fails
Expand Down
54 changes: 48 additions & 6 deletions influxdb/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ async fn test_wrong_authed_write_and_read() {
let write_result = client.query(write_query).await;
assert_result_err(&write_result);
match write_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
msrd0 marked this conversation as resolved.
Show resolved Hide resolved
_ => panic!(
"Should be an AuthorizationError: {}",
msrd0 marked this conversation as resolved.
Show resolved Hide resolved
write_result.unwrap_err()
Expand All @@ -151,7 +158,14 @@ async fn test_wrong_authed_write_and_read() {
let read_result = client.query(read_query).await;
assert_result_err(&read_result);
match read_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
read_result.unwrap_err()
Expand All @@ -164,7 +178,14 @@ async fn test_wrong_authed_write_and_read() {
let read_result = client.query(read_query).await;
assert_result_err(&read_result);
match read_result {
Err(Error::AuthenticationError) => {}
Err(Error::ApiError(code)) => {
if code != 401 {
panic!(
"Should be an ApiError(401), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthenticationError: {}",
read_result.unwrap_err()
Expand Down Expand Up @@ -208,7 +229,14 @@ async fn test_non_authed_write_and_read() {
let write_result = non_authed_client.query(write_query).await;
assert_result_err(&write_result);
match write_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
write_result.unwrap_err()
Expand All @@ -220,7 +248,14 @@ async fn test_non_authed_write_and_read() {

assert_result_err(&read_result);
match read_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
read_result.unwrap_err()
Expand Down Expand Up @@ -297,7 +332,14 @@ async fn test_json_non_authed_read() {
let read_result = non_authed_client.json_query(read_query).await;
assert_result_err(&read_result);
match read_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be a AuthorizationError: {}",
read_result.unwrap_err()
Expand Down
36 changes: 32 additions & 4 deletions influxdb/tests/integration_tests_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ async fn test_wrong_authed_write_and_read() {
let write_result = client.query(&write_query).await;
assert_result_err(&write_result);
match write_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
write_result.unwrap_err()
Expand All @@ -68,7 +75,14 @@ async fn test_wrong_authed_write_and_read() {
let read_result = client.query(&read_query).await;
assert_result_err(&read_result);
match read_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
read_result.unwrap_err()
Expand All @@ -95,7 +109,14 @@ async fn test_non_authed_write_and_read() {
let write_result = non_authed_client.query(&write_query).await;
assert_result_err(&write_result);
match write_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
write_result.unwrap_err()
Expand All @@ -106,7 +127,14 @@ async fn test_non_authed_write_and_read() {
let read_result = non_authed_client.query(&read_query).await;
assert_result_err(&read_result);
match read_result {
Err(Error::AuthorizationError) => {}
Err(Error::ApiError(code)) => {
if code != 403 {
panic!(
"Should be an ApiError(403), but code received was: {}",
code
);
}
}
_ => panic!(
"Should be an AuthorizationError: {}",
read_result.unwrap_err()
Expand Down
Loading