From 80048b21a712fa82b8838fce244faed4877e85f2 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Fri, 14 Jul 2023 16:13:09 +0200 Subject: [PATCH] switch Result to anyhow from custom --- Cargo.toml | 2 ++ google_bigquery_v2_derive/src/lib.rs | 2 +- src/data/bigquery_table.rs | 17 ++++++++++------- .../param_conversion/convert_bigquery_params.rs | 4 ++-- src/data/query_builder.rs | 12 +++++++----- src/prelude.rs | 2 +- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86d0d5b..bbbc11c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ tokio = "1.0.2" google_bigquery_v2_derive = { version = "0.0.2", path = "./google_bigquery_v2_derive" } chrono = "0.4.24" nameof = "1.2.2" +anyhow = "1.0" +thiserror = "1.0" env_logger = "0.10.0" async-trait = "0.1.68" diff --git a/google_bigquery_v2_derive/src/lib.rs b/google_bigquery_v2_derive/src/lib.rs index a151a8d..034f36a 100644 --- a/google_bigquery_v2_derive/src/lib.rs +++ b/google_bigquery_v2_derive/src/lib.rs @@ -115,7 +115,7 @@ fn implement_get_parameter_from_field(ast: &DeriveInput, table_ident: &Ident) -> google_bigquery_v2::prelude::trace!("get_parameter_from_field(); field_name: '{}' self:{:?}", field_name, self); match field_name { #(#fields)* - _ => Err(format!("Field {} not found", field_name).into()), + _ => Err(google_bigquery_v2::prelude::anyhow!("Field {} not found", field_name).into()), } } } diff --git a/src/data/bigquery_table.rs b/src/data/bigquery_table.rs index 97aa1a5..1c07113 100644 --- a/src/data/bigquery_table.rs +++ b/src/data/bigquery_table.rs @@ -131,7 +131,7 @@ pub trait BigQueryTable: BigQueryTableBase { let query_fields = Self::get_query_fields(true); let db_name = query_fields.get(field_name); match db_name { - None => Err(format!("Field {} not found.", field_name).into()), + None => Err(anyhow!("Field {} not found.", field_name).into()), Some(s) => Ok(s.to_string()), } } @@ -168,20 +168,23 @@ pub trait BigQueryTable: BigQueryTableBase { let mut rows = match result { QueryResultType::WithRowData(data) => data, QueryResultType::WithoutRowData(success) => { - return Err(format!( + return Err(anyhow!( "something went wrong when getting for {} = {:?};\tresult: {:?}", - pk_field_name, pk_value, success + pk_field_name, + pk_value, + success ) .into()); } }; if rows.len() == 0 { - Err(format!("No entry found for {} = {:?}", pk_db_name, pk_value).into()) + Err(anyhow!("No entry found for {} = {:?}", pk_db_name, pk_value).into()) } else if rows.len() > 1 { - Err(format!( + Err(anyhow!( "More than one entry found for {} = {:?}", - pk_db_name, pk_value + pk_db_name, + pk_value ) .into()) } else { @@ -233,7 +236,7 @@ pub trait BigQueryTable: BigQueryTableBase { if count == 0 { Ok(()) } else { - Err(format!( + Err(anyhow!( "save should return empty data, but returned {} rows.", count ) diff --git a/src/data/param_conversion/convert_bigquery_params.rs b/src/data/param_conversion/convert_bigquery_params.rs index 57e8c68..f32c0ff 100644 --- a/src/data/param_conversion/convert_bigquery_params.rs +++ b/src/data/param_conversion/convert_bigquery_params.rs @@ -40,7 +40,7 @@ impl ConvertBigQueryParams for bool { "true" => Ok(true), "FALSE" => Ok(false), "false" => Ok(false), - invalid => Err(format!("Invalid value for bool: '{}'", invalid).into()), + invalid => Err(anyhow!("Invalid value for bool: '{}'", invalid)), } } fn to_param(&self) -> Value { @@ -138,7 +138,7 @@ pub fn convert_value_to_string(value: Value) -> Result { Ok(value::from_value(value)?) } else { match value { - Value::Null => Err("Value is Null".into()), + Value::Null => Err(anyhow!("Value is Null")), Value::Number(num) => Ok(num.to_string()), Value::String(s) => Ok(s), _ => { diff --git a/src/data/query_builder.rs b/src/data/query_builder.rs index 963e497..b86301d 100644 --- a/src/data/query_builder.rs +++ b/src/data/query_builder.rs @@ -50,7 +50,7 @@ impl QueryResultType { match self { QueryResultType::WithRowData(data) => Ok(data), QueryResultType::WithoutRowData(_) => { - Err(format!("map_err_with_data message:{}", message.into()).into()) + Err(anyhow!("map_err_with_data message:{}", message.into())) } } } @@ -61,12 +61,11 @@ impl QueryResultType { if data.len() == 0 { return Ok(()); } - return Err(format!( + return Err(anyhow!( "map_err_without_data message:'{}' data: {:?}", message.into(), data - ) - .into()); + )); } } } @@ -779,7 +778,10 @@ async fn run_query_with_client( .await?; if response.status() != 200 { - return Err(format!("Wrong status code returned! ({})", response.status()).into()); + return Err(anyhow!( + "Wrong status code returned! ({})", + response.status() + )); } Ok((response, query_response)) diff --git a/src/prelude.rs b/src/prelude.rs index abf7c26..cf61045 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,6 +3,6 @@ pub use google_bigquery_v2_derive::BigDataTableDerive; pub use crate::client::BigqueryClient; pub use crate::data::{BigQueryTable, BigQueryTableBase, OrderDirection}; -pub type Result = std::result::Result>; +pub use anyhow::{anyhow, Result}; pub use tracing::{debug, error, info, trace, warn};