From 5c9b6bda36fa52d9499a69b0310747cb3e5f9ca9 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Tue, 5 Mar 2024 14:37:36 +0000 Subject: [PATCH 01/11] better errors --- influxdb/src/client/mod.rs | 11 ++++------- influxdb/src/error.rs | 14 +++++--------- influxdb/tests/integration_tests.rs | 24 ++++++++++++++++++------ influxdb/tests/integration_tests_v2.rs | 16 ++++++++++++---- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index ad198bc..5446f0d 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -281,7 +281,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), }); @@ -301,12 +301,9 @@ 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(()) + match status { + StatusCode::OK => Ok(()), + code => Err(Error::ApiError(code)), } } diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index b2f651b..0f9267a 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -1,8 +1,9 @@ //! Errors that might happen in the crate +use reqwest::StatusCode; use thiserror::Error; -#[derive(Debug, Eq, PartialEq, Error)] +#[derive(Debug, Error)] pub enum Error { #[error("query is invalid: {error}")] /// Error happens when a query is invalid @@ -24,15 +25,10 @@ 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("connection error: {error}")] /// Error happens when HTTP request fails ConnectionError { error: String }, + + #[error("server responded with an error code: {0}")] + ApiError(StatusCode), } diff --git a/influxdb/tests/integration_tests.rs b/influxdb/tests/integration_tests.rs index e3750cc..955d67a 100644 --- a/influxdb/tests/integration_tests.rs +++ b/influxdb/tests/integration_tests.rs @@ -140,7 +140,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -151,7 +153,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -164,7 +168,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthenticationError: {}", read_result.unwrap_err() @@ -208,7 +214,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -220,7 +228,9 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { - Err(Error::AuthorizationError) => {} + Err(Error::ApiError(code)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -297,7 +307,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be a AuthorizationError: {}", read_result.unwrap_err() diff --git a/influxdb/tests/integration_tests_v2.rs b/influxdb/tests/integration_tests_v2.rs index 74e17a7..f70347c 100644 --- a/influxdb/tests/integration_tests_v2.rs +++ b/influxdb/tests/integration_tests_v2.rs @@ -57,7 +57,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -68,7 +70,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -95,7 +99,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -106,7 +112,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() From 14728a2c85265e032428e2ea39c976874ec11033 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 19:22:07 +0000 Subject: [PATCH 02/11] change reqwest::StatusCode to http::StatusCode --- influxdb/src/error.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 0f9267a..23d3cf7 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -1,6 +1,6 @@ //! Errors that might happen in the crate -use reqwest::StatusCode; +use http::StatusCode; use thiserror::Error; #[derive(Debug, Error)] @@ -29,6 +29,7 @@ pub enum Error { /// Error happens when HTTP request fails ConnectionError { error: String }, + #[cfg(feature = "reqwest")] #[error("server responded with an error code: {0}")] ApiError(StatusCode), } From 986a0137c34845e81fa7a7277d888cc34b57b66f Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 19:22:58 +0000 Subject: [PATCH 03/11] handle all 2xx responses as success --- influxdb/src/client/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 5446f0d..303c7da 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -303,7 +303,13 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { let status = res.status(); match status { StatusCode::OK => Ok(()), - code => Err(Error::ApiError(code)), + code => { + if code.as_str().as_bytes()[0] == b'2' { + return Ok(()); + } else { + return Err(Error::ApiError(code)); + } + } } } From 58742bc163644bf067d6459d2a7f09a34b3a2e14 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 20:58:49 +0000 Subject: [PATCH 04/11] remove dbg from tests --- influxdb/src/client/mod.rs | 13 ++--- influxdb/src/error.rs | 1 - influxdb/tests/integration_tests.rs | 72 +++++++++++++++----------- influxdb/tests/integration_tests_v2.rs | 48 ++++++++++------- 4 files changed, 73 insertions(+), 61 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 303c7da..164e4d3 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -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}; @@ -301,15 +300,9 @@ impl Client { pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { let status = res.status(); - match status { - StatusCode::OK => Ok(()), - code => { - if code.as_str().as_bytes()[0] == b'2' { - return Ok(()); - } else { - return Err(Error::ApiError(code)); - } - } + match status.is_success() { + true => Ok(()), + false => Err(Error::ApiError(status)), } } diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 23d3cf7..40143a9 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -29,7 +29,6 @@ pub enum Error { /// Error happens when HTTP request fails ConnectionError { error: String }, - #[cfg(feature = "reqwest")] #[error("server responded with an error code: {0}")] ApiError(StatusCode), } diff --git a/influxdb/tests/integration_tests.rs b/influxdb/tests/integration_tests.rs index 955d67a..4839685 100644 --- a/influxdb/tests/integration_tests.rs +++ b/influxdb/tests/integration_tests.rs @@ -141,12 +141,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -154,12 +156,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } let client = Client::new("http://127.0.0.1:9086", TEST_NAME) @@ -169,12 +173,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 403_u16 { + panic!( + "Should be an ApiError(403), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthenticationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(403): {}", read_result.unwrap_err()), } }, || async move { @@ -215,12 +221,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -229,12 +237,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move { @@ -308,12 +318,14 @@ async fn test_json_non_authed_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be a AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move { diff --git a/influxdb/tests/integration_tests_v2.rs b/influxdb/tests/integration_tests_v2.rs index f70347c..a5a7608 100644 --- a/influxdb/tests/integration_tests_v2.rs +++ b/influxdb/tests/integration_tests_v2.rs @@ -58,12 +58,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -71,12 +73,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move {}, @@ -100,12 +104,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -113,12 +119,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move {}, From 8fbcf6ccb3e401e360bdf90673c124add351d6a4 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Tue, 5 Mar 2024 14:37:36 +0000 Subject: [PATCH 05/11] better errors --- influxdb/src/client/mod.rs | 11 ++++------- influxdb/src/error.rs | 12 ++++-------- influxdb/tests/integration_tests.rs | 24 ++++++++++++++++++------ influxdb/tests/integration_tests_v2.rs | 16 ++++++++++++---- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index ad198bc..5446f0d 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -281,7 +281,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), }); @@ -301,12 +301,9 @@ 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(()) + match status { + StatusCode::OK => Ok(()), + code => Err(Error::ApiError(code)), } } diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 8bfd043..0cff9d4 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -1,5 +1,6 @@ //! Errors that might happen in the crate +use reqwest::StatusCode; use thiserror::Error; #[derive(Debug, Eq, PartialEq, Error)] @@ -25,15 +26,10 @@ 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("connection error: {error}")] /// Error happens when HTTP request fails ConnectionError { error: String }, + + #[error("server responded with an error code: {0}")] + ApiError(StatusCode), } diff --git a/influxdb/tests/integration_tests.rs b/influxdb/tests/integration_tests.rs index f392f95..5ecc920 100644 --- a/influxdb/tests/integration_tests.rs +++ b/influxdb/tests/integration_tests.rs @@ -140,7 +140,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -151,7 +153,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -164,7 +168,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthenticationError: {}", read_result.unwrap_err() @@ -208,7 +214,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -220,7 +228,9 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { - Err(Error::AuthorizationError) => {} + Err(Error::ApiError(code)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -297,7 +307,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be a AuthorizationError: {}", read_result.unwrap_err() diff --git a/influxdb/tests/integration_tests_v2.rs b/influxdb/tests/integration_tests_v2.rs index 74e17a7..f70347c 100644 --- a/influxdb/tests/integration_tests_v2.rs +++ b/influxdb/tests/integration_tests_v2.rs @@ -57,7 +57,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -68,7 +70,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() @@ -95,7 +99,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", write_result.unwrap_err() @@ -106,7 +112,9 @@ 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)) => { + dbg!(code); + } _ => panic!( "Should be an AuthorizationError: {}", read_result.unwrap_err() From d583ef3de4b28ef5bfcb5056f450f4bc609fea05 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 19:22:07 +0000 Subject: [PATCH 06/11] change reqwest::StatusCode to http::StatusCode --- influxdb/src/error.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 0cff9d4..9bcea2f 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -1,6 +1,6 @@ //! Errors that might happen in the crate -use reqwest::StatusCode; +use http::StatusCode; use thiserror::Error; #[derive(Debug, Eq, PartialEq, Error)] @@ -30,6 +30,7 @@ pub enum Error { /// Error happens when HTTP request fails ConnectionError { error: String }, + #[cfg(feature = "reqwest")] #[error("server responded with an error code: {0}")] ApiError(StatusCode), } From f2391dd5276deae9e97887a160a19bb7e98dded1 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 19:22:58 +0000 Subject: [PATCH 07/11] handle all 2xx responses as success --- influxdb/src/client/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 5446f0d..303c7da 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -303,7 +303,13 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { let status = res.status(); match status { StatusCode::OK => Ok(()), - code => Err(Error::ApiError(code)), + code => { + if code.as_str().as_bytes()[0] == b'2' { + return Ok(()); + } else { + return Err(Error::ApiError(code)); + } + } } } From 1603bfaad628a5831343217f0c31f11067d9b5e2 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 20:58:49 +0000 Subject: [PATCH 08/11] remove dbg from tests --- influxdb/src/client/mod.rs | 13 ++--- influxdb/src/error.rs | 1 - influxdb/tests/integration_tests.rs | 72 +++++++++++++++----------- influxdb/tests/integration_tests_v2.rs | 48 ++++++++++------- 4 files changed, 73 insertions(+), 61 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 303c7da..164e4d3 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -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}; @@ -301,15 +300,9 @@ impl Client { pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { let status = res.status(); - match status { - StatusCode::OK => Ok(()), - code => { - if code.as_str().as_bytes()[0] == b'2' { - return Ok(()); - } else { - return Err(Error::ApiError(code)); - } - } + match status.is_success() { + true => Ok(()), + false => Err(Error::ApiError(status)), } } diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 9bcea2f..0efc4a6 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -30,7 +30,6 @@ pub enum Error { /// Error happens when HTTP request fails ConnectionError { error: String }, - #[cfg(feature = "reqwest")] #[error("server responded with an error code: {0}")] ApiError(StatusCode), } diff --git a/influxdb/tests/integration_tests.rs b/influxdb/tests/integration_tests.rs index 5ecc920..cd35b3c 100644 --- a/influxdb/tests/integration_tests.rs +++ b/influxdb/tests/integration_tests.rs @@ -141,12 +141,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -154,12 +156,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } let client = Client::new("http://127.0.0.1:9086", TEST_NAME) @@ -169,12 +173,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 403_u16 { + panic!( + "Should be an ApiError(403), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthenticationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(403): {}", read_result.unwrap_err()), } }, || async move { @@ -215,12 +221,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -229,12 +237,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move { @@ -308,12 +318,14 @@ async fn test_json_non_authed_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be a AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move { diff --git a/influxdb/tests/integration_tests_v2.rs b/influxdb/tests/integration_tests_v2.rs index f70347c..a5a7608 100644 --- a/influxdb/tests/integration_tests_v2.rs +++ b/influxdb/tests/integration_tests_v2.rs @@ -58,12 +58,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -71,12 +73,14 @@ async fn test_wrong_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move {}, @@ -100,12 +104,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&write_result); match write_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - write_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", write_result.unwrap_err()), } let read_query = ReadQuery::new("SELECT * FROM weather"); @@ -113,12 +119,14 @@ async fn test_non_authed_write_and_read() { assert_result_err(&read_result); match read_result { Err(Error::ApiError(code)) => { - dbg!(code); + if code.as_u16() != 401_u16 { + panic!( + "Should be an ApiError(401), but code received was: {}", + code + ); + }; } - _ => panic!( - "Should be an AuthorizationError: {}", - read_result.unwrap_err() - ), + _ => panic!("Should be an ApiError(401): {}", read_result.unwrap_err()), } }, || async move {}, From 456499b6f891beee7c5bc0a668d084137f38ce59 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 23:47:38 +0000 Subject: [PATCH 09/11] bump --- influxdb/src/client/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 164e4d3..dfb553b 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -299,7 +299,7 @@ impl Client { } pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { - let status = res.status(); + let status: http::StatusCode = res.status(); match status.is_success() { true => Ok(()), false => Err(Error::ApiError(status)), From 3544ded46d4bfe3011eb454f2edbd4c25fb221a3 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 23:51:16 +0000 Subject: [PATCH 10/11] bump --- influxdb/src/client/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index dfb553b..ce0036e 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -299,10 +299,10 @@ impl Client { } pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { - let status: http::StatusCode = res.status(); + let status = res.status(); match status.is_success() { true => Ok(()), - false => Err(Error::ApiError(status)), + false => Err(Error::ApiError(status.into())), } } From 49b29229190cfa9833d3ab04c855649c9b97d797 Mon Sep 17 00:00:00 2001 From: 0xDmtri <0xDmtri@protonmail.com> Date: Sun, 10 Mar 2024 23:57:26 +0000 Subject: [PATCH 11/11] bump --- influxdb/src/client/mod.rs | 2 +- influxdb/src/error.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index ce0036e..164e4d3 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -302,7 +302,7 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> { let status = res.status(); match status.is_success() { true => Ok(()), - false => Err(Error::ApiError(status.into())), + false => Err(Error::ApiError(status)), } } diff --git a/influxdb/src/error.rs b/influxdb/src/error.rs index 0efc4a6..73bc38e 100644 --- a/influxdb/src/error.rs +++ b/influxdb/src/error.rs @@ -1,6 +1,9 @@ //! Errors that might happen in the crate -use http::StatusCode; +#[cfg(feature = "reqwest")] +use reqwest::StatusCode; +#[cfg(feature = "surf")] +use surf::StatusCode; use thiserror::Error; #[derive(Debug, Eq, PartialEq, Error)]