switch Result to anyhow from custom

This commit is contained in:
OMGeeky
2023-07-14 16:13:09 +02:00
parent ad8dd1bec4
commit 80048b21a7
6 changed files with 23 additions and 16 deletions

View File

@@ -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"

View File

@@ -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()),
}
}
}

View File

@@ -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
)

View File

@@ -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<String> {
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),
_ => {

View File

@@ -50,7 +50,7 @@ impl<T: Debug> QueryResultType<T> {
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<T: Debug> QueryResultType<T> {
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))

View File

@@ -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<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub use anyhow::{anyhow, Result};
pub use tracing::{debug, error, info, trace, warn};