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" } google_bigquery_v2_derive = { version = "0.0.2", path = "./google_bigquery_v2_derive" }
chrono = "0.4.24" chrono = "0.4.24"
nameof = "1.2.2" nameof = "1.2.2"
anyhow = "1.0"
thiserror = "1.0"
env_logger = "0.10.0" env_logger = "0.10.0"
async-trait = "0.1.68" 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); google_bigquery_v2::prelude::trace!("get_parameter_from_field(); field_name: '{}' self:{:?}", field_name, self);
match field_name { match field_name {
#(#fields)* #(#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 query_fields = Self::get_query_fields(true);
let db_name = query_fields.get(field_name); let db_name = query_fields.get(field_name);
match db_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()), Some(s) => Ok(s.to_string()),
} }
} }
@@ -168,20 +168,23 @@ pub trait BigQueryTable: BigQueryTableBase {
let mut rows = match result { let mut rows = match result {
QueryResultType::WithRowData(data) => data, QueryResultType::WithRowData(data) => data,
QueryResultType::WithoutRowData(success) => { QueryResultType::WithoutRowData(success) => {
return Err(format!( return Err(anyhow!(
"something went wrong when getting for {} = {:?};\tresult: {:?}", "something went wrong when getting for {} = {:?};\tresult: {:?}",
pk_field_name, pk_value, success pk_field_name,
pk_value,
success
) )
.into()); .into());
} }
}; };
if rows.len() == 0 { 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 { } else if rows.len() > 1 {
Err(format!( Err(anyhow!(
"More than one entry found for {} = {:?}", "More than one entry found for {} = {:?}",
pk_db_name, pk_value pk_db_name,
pk_value
) )
.into()) .into())
} else { } else {
@@ -233,7 +236,7 @@ pub trait BigQueryTable: BigQueryTableBase {
if count == 0 { if count == 0 {
Ok(()) Ok(())
} else { } else {
Err(format!( Err(anyhow!(
"save should return empty data, but returned {} rows.", "save should return empty data, but returned {} rows.",
count count
) )

View File

@@ -40,7 +40,7 @@ impl ConvertBigQueryParams for bool {
"true" => Ok(true), "true" => Ok(true),
"FALSE" => Ok(false), "FALSE" => Ok(false),
"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 { fn to_param(&self) -> Value {
@@ -138,7 +138,7 @@ pub fn convert_value_to_string(value: Value) -> Result<String> {
Ok(value::from_value(value)?) Ok(value::from_value(value)?)
} else { } else {
match value { match value {
Value::Null => Err("Value is Null".into()), Value::Null => Err(anyhow!("Value is Null")),
Value::Number(num) => Ok(num.to_string()), Value::Number(num) => Ok(num.to_string()),
Value::String(s) => Ok(s), Value::String(s) => Ok(s),
_ => { _ => {

View File

@@ -50,7 +50,7 @@ impl<T: Debug> QueryResultType<T> {
match self { match self {
QueryResultType::WithRowData(data) => Ok(data), QueryResultType::WithRowData(data) => Ok(data),
QueryResultType::WithoutRowData(_) => { 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 { if data.len() == 0 {
return Ok(()); return Ok(());
} }
return Err(format!( return Err(anyhow!(
"map_err_without_data message:'{}' data: {:?}", "map_err_without_data message:'{}' data: {:?}",
message.into(), message.into(),
data data
) ));
.into());
} }
} }
} }
@@ -779,7 +778,10 @@ async fn run_query_with_client(
.await?; .await?;
if response.status() != 200 { 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)) 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::client::BigqueryClient;
pub use crate::data::{BigQueryTable, BigQueryTableBase, OrderDirection}; 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}; pub use tracing::{debug, error, info, trace, warn};