improve error handling part 4 - added thiserror
parent
f84387d238
commit
441ce5c630
|
@ -1089,7 +1089,7 @@ dependencies = [
|
||||||
"sha2",
|
"sha2",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"subprocess",
|
"subprocess",
|
||||||
"thiserror",
|
"thiserror 1.0.69",
|
||||||
"time",
|
"time",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
@ -1358,6 +1358,7 @@ dependencies = [
|
||||||
"mysql",
|
"mysql",
|
||||||
"sprintf",
|
"sprintf",
|
||||||
"termsize",
|
"termsize",
|
||||||
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1369,7 +1370,7 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom",
|
"getrandom",
|
||||||
"libredox",
|
"libredox",
|
||||||
"thiserror",
|
"thiserror 1.0.69",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1708,7 +1709,16 @@ version = "1.0.69"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl 1.0.69",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "2.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl 2.0.12",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1722,6 +1732,17 @@ dependencies = [
|
||||||
"syn 2.0.90",
|
"syn 2.0.90",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "2.0.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.90",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.37"
|
version = "0.3.37"
|
||||||
|
|
|
@ -15,4 +15,5 @@ log = "0.4.17"
|
||||||
mysql = "23.0.1"
|
mysql = "23.0.1"
|
||||||
sprintf = "0.1.3"
|
sprintf = "0.1.3"
|
||||||
termsize = "0.1.6"
|
termsize = "0.1.6"
|
||||||
|
thiserror = "2.0.12"
|
||||||
time = "0.3.20"
|
time = "0.3.20"
|
||||||
|
|
|
@ -9,15 +9,23 @@ use crate::{
|
||||||
|
|
||||||
use super::command::{Cli, Commands};
|
use super::command::{Cli, Commands};
|
||||||
|
|
||||||
pub fn process_args(args: Cli, db: Db) {
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
enum CommandError {
|
||||||
|
#[error("Empty search provided.")]
|
||||||
|
EmptySearch(),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_args(args: Cli, db: Db) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
match args.mode {
|
match args.mode {
|
||||||
Commands::Merch { search } => {
|
Commands::Merch { search } => {
|
||||||
let mut merchant_service: MerchantService = container::get_merchant_service(db);
|
let mut merchant_service: MerchantService = container::get_merchant_service(db);
|
||||||
merchant_service.get_merchant(&search.unwrap());
|
merchant_service.get_merchant(&search.ok_or(CommandError::EmptySearch())?);
|
||||||
}
|
}
|
||||||
Commands::Schema { search } => {
|
Commands::Schema { search } => {
|
||||||
let mut schema_service: SchemaService = container::get_schema_service(db);
|
let mut schema_service: SchemaService = container::get_schema_service(db);
|
||||||
schema_service.get_columns(&search.unwrap())
|
schema_service.get_columns(&search.ok_or(CommandError::EmptySearch())?)
|
||||||
}
|
}
|
||||||
Commands::Filter {
|
Commands::Filter {
|
||||||
filter_id,
|
filter_id,
|
||||||
|
@ -47,4 +55,6 @@ pub fn process_args(args: Cli, db: Db) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,11 @@ impl Db {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let location: String = env::var("LOCATION").unwrap_or_else(|_| "localhost".to_string());
|
let location: String = env::var("LOCATION").unwrap_or_else(|_| "localhost".to_string());
|
||||||
let user: String = env::var("USERNAME").unwrap();
|
let user: String = env::var("USERNAME")
|
||||||
|
.map_err(|_| Box::new(InitError("Missing USERNAME in dotenv file.".to_string())))?;
|
||||||
let port: String = env::var("PORT").unwrap_or_else(|_| "3306".to_string());
|
let port: String = env::var("PORT").unwrap_or_else(|_| "3306".to_string());
|
||||||
let password: String = env::var("PASSWORD").unwrap();
|
let password: String = env::var("PASSWORD")
|
||||||
|
.map_err(|_| Box::new(InitError("Missing PASSWORD in dotenv file.".to_string())))?;
|
||||||
let db_name: String = env::var("DB").unwrap_or_else(|_| "efulfilment".to_string());
|
let db_name: String = env::var("DB").unwrap_or_else(|_| "efulfilment".to_string());
|
||||||
let url: String = format!(
|
let url: String = format!(
|
||||||
"mysql://{}:{}@{}:{}/{}",
|
"mysql://{}:{}@{}:{}/{}",
|
||||||
|
@ -58,7 +60,7 @@ impl Db {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_connection(&self) -> Result<PooledConn, mysql::Error> {
|
pub fn get_connection(&self) -> Result<PooledConn, mysql::Error> {
|
||||||
let pool = self.pool.lock().unwrap();
|
let pool = self.pool.lock()?;
|
||||||
pool.get_conn()
|
pool.get_conn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||||
let env: &String = &Cli::parse().env.unwrap_or("local".to_string());
|
let env: &String = &Cli::parse().env.unwrap_or("local".to_string());
|
||||||
let db: Db = Db::initialize(env.to_string())?;
|
let db: Db = Db::initialize(env.to_string())?;
|
||||||
|
|
||||||
controller::process_args(args, db);
|
controller::process_args(args, db)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,7 @@ impl FilterRepo {
|
||||||
FROM filter f
|
FROM filter f
|
||||||
JOIN global_data.filter_modules fm ON fm.filter_module_id = f.filter_module_id
|
JOIN global_data.filter_modules fm ON fm.filter_module_id = f.filter_module_id
|
||||||
WHERE f.filter_id = :filter_id
|
WHERE f.filter_id = :filter_id
|
||||||
")
|
")?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let params = params! {"filter_id" => filter_id};
|
let params = params! {"filter_id" => filter_id};
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
|
@ -52,8 +51,7 @@ impl FilterRepo {
|
||||||
JOIN global_data.effi_user t2 on t1.upd_user = t2.user_id
|
JOIN global_data.effi_user t2 on t1.upd_user = t2.user_id
|
||||||
WHERE t1.filter_id = :filter_id
|
WHERE t1.filter_id = :filter_id
|
||||||
",
|
",
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
stat,
|
stat,
|
||||||
|
@ -70,15 +68,13 @@ impl FilterRepo {
|
||||||
|
|
||||||
pub fn find_filter_log(&mut self, filter_id: &usize) -> Result<Vec<FilterLog>, mysql::Error> {
|
pub fn find_filter_log(&mut self, filter_id: &usize) -> Result<Vec<FilterLog>, mysql::Error> {
|
||||||
let mut connection: PooledConn = self.db.get_connection()?;
|
let mut connection: PooledConn = self.db.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection.prep(
|
||||||
.prep(
|
"
|
||||||
"
|
|
||||||
SELECT t1.run_ts, t1.error_code, t1.error_msg, t1.mysql_error
|
SELECT t1.run_ts, t1.error_code, t1.error_msg, t1.mysql_error
|
||||||
FROM filter_run_error_log t1
|
FROM filter_run_error_log t1
|
||||||
WHERE t1.filter_id = :filter_id
|
WHERE t1.filter_id = :filter_id
|
||||||
",
|
",
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
stat,
|
stat,
|
||||||
|
|
|
@ -17,17 +17,15 @@ impl JobRepo {
|
||||||
|
|
||||||
pub fn find_by_id(&mut self, job_id: &usize) -> Result<Vec<Job>, mysql::Error> {
|
pub fn find_by_id(&mut self, job_id: &usize) -> Result<Vec<Job>, mysql::Error> {
|
||||||
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection.prep(
|
||||||
.prep(
|
"SELECT
|
||||||
"SELECT
|
|
||||||
jm.file_name, jm.description
|
jm.file_name, jm.description
|
||||||
FROM jobs j
|
FROM jobs j
|
||||||
JOIN global_data.job_modules jm ON jm.job_module_id = j.job_module_id
|
JOIN global_data.job_modules jm ON jm.job_module_id = j.job_module_id
|
||||||
WHERE
|
WHERE
|
||||||
j.job_id = :job_id
|
j.job_id = :job_id
|
||||||
",
|
",
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
stat,
|
stat,
|
||||||
|
@ -41,9 +39,8 @@ impl JobRepo {
|
||||||
|
|
||||||
pub fn find_log(&mut self, job_id: &usize) -> Result<Vec<JobLog>, mysql::Error> {
|
pub fn find_log(&mut self, job_id: &usize) -> Result<Vec<JobLog>, mysql::Error> {
|
||||||
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection.prep(
|
||||||
.prep(
|
"
|
||||||
"
|
|
||||||
SELECT jea.content, jea.upd_ts
|
SELECT jea.content, jea.upd_ts
|
||||||
FROM jobs j
|
FROM jobs j
|
||||||
JOIN job_events je on j.job_id = je.job_id AND je.m_id = j.m_id
|
JOIN job_events je on j.job_id = je.job_id AND je.m_id = j.m_id
|
||||||
|
@ -51,8 +48,7 @@ impl JobRepo {
|
||||||
WHERE
|
WHERE
|
||||||
j.job_id = :job_id
|
j.job_id = :job_id
|
||||||
",
|
",
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(stat, params! {"job_id" => job_id}, |(content, upd_ts)| {
|
connection.exec_map(stat, params! {"job_id" => job_id}, |(content, upd_ts)| {
|
||||||
JobLog { content, upd_ts }
|
JobLog { content, upd_ts }
|
||||||
|
|
|
@ -14,8 +14,7 @@ impl MerchantRepo {
|
||||||
pub fn find_by_name_or_id(&mut self, search: &str) -> Result<Vec<Merchant>, mysql::Error> {
|
pub fn find_by_name_or_id(&mut self, search: &str) -> Result<Vec<Merchant>, mysql::Error> {
|
||||||
let mut connection = self.db_pool.get_connection()?;
|
let mut connection = self.db_pool.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection
|
||||||
.prep("SELECT m_name, m_id FROM global_data.merchant WHERE m_id = :search OR m_name LIKE :search2;")
|
.prep("SELECT m_name, m_id FROM global_data.merchant WHERE m_id = :search OR m_name LIKE :search2;")?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let search_like = format!("%{}%", search);
|
let search_like = format!("%{}%", search);
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,7 @@ impl SchemaRepo {
|
||||||
pub fn find_by_column(&mut self, column_name: &str) -> Result<Vec<Schema>, mysql::Error> {
|
pub fn find_by_column(&mut self, column_name: &str) -> Result<Vec<Schema>, mysql::Error> {
|
||||||
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
let mut connection: mysql::PooledConn = self.db.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection
|
||||||
.prep("SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE :c_name")
|
.prep("SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE :c_name")?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
stat,
|
stat,
|
||||||
|
|
Loading…
Reference in New Issue