From 42aa8228b88382622a067db2025298d94022c1e9 Mon Sep 17 00:00:00 2001 From: mace Date: Mon, 17 Mar 2025 15:18:03 +0100 Subject: [PATCH] error handling improved, start with env --- src/cli/command.rs | 8 +- src/cli/controller.rs | 25 +++-- src/container.rs | 10 +- src/service/file_service.rs | 30 +++-- src/service/filter_service.rs | 188 ++++++++++++++++---------------- src/service/job_service.rs | 54 ++++----- src/service/merchant_service.rs | 29 +++-- src/service/mod.rs | 7 +- src/service/schema_service.rs | 31 +++--- 9 files changed, 189 insertions(+), 193 deletions(-) diff --git a/src/cli/command.rs b/src/cli/command.rs index e19a1d8..271a625 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -35,11 +35,15 @@ pub enum Commands { }, Page { // #[arg(short, long)] - page_id: usize + page_id: usize, }, Job { job_id: usize, #[arg(short, long)] log: bool, - } + }, + Env { + env: Option, + target: Option, + }, } diff --git a/src/cli/controller.rs b/src/cli/controller.rs index dd6de6b..f8ec1e6 100644 --- a/src/cli/controller.rs +++ b/src/cli/controller.rs @@ -2,8 +2,8 @@ use crate::{ container, database::db::Db, service::{ - file_service::FileService, filter_service::FilterService, job_service::JobService, - merchant_service::MerchantService, schema_service::SchemaService, + env_service::EnvService, file_service::FileService, filter_service::FilterService, + job_service::JobService, merchant_service::MerchantService, schema_service::SchemaService, }, }; @@ -21,11 +21,11 @@ pub fn process_args(args: Cli, db: Db) -> Result<(), Box> match args.mode { Commands::Merch { search } => { let mut merchant_service: MerchantService = container::get_merchant_service(db); - merchant_service.get_merchant(&search.ok_or(CommandError::EmptySearch())?); + merchant_service.get_merchant(&search.ok_or(CommandError::EmptySearch())?)?; } Commands::Schema { search } => { let mut schema_service: SchemaService = container::get_schema_service(db); - schema_service.get_columns(&search.ok_or(CommandError::EmptySearch())?) + schema_service.get_columns(&search.ok_or(CommandError::EmptySearch())?)?; } Commands::Filter { filter_id, @@ -35,25 +35,30 @@ pub fn process_args(args: Cli, db: Db) -> Result<(), Box> } => { let mut filter_service: FilterService = container::get_filter_service(db); if config { - filter_service.get_filter_configs(&filter_id) + filter_service.get_filter_configs(&filter_id)?; } else if log { - filter_service.get_filter_log(&filter_id, all) + filter_service.get_filter_log(&filter_id, all)?; } else { - filter_service.get_filter(&filter_id, all) + filter_service.get_filter(&filter_id, all)?; } } Commands::Page { page_id } => { let mut file_service: FileService = container::get_page_service(db); - file_service.get_page(&page_id); + file_service.get_page(&page_id)?; } Commands::Job { job_id, log } => { let mut job_service: JobService = container::get_job_service(db); if log { - job_service.get_job_log(&job_id); + job_service.get_job_log(&job_id)?; } else { - job_service.get_job_by_id(&job_id); + job_service.get_job_by_id(&job_id)?; } } + Commands::Env { env, target } => { + EnvService::set_environment()?; + + println!("{:?}, {:?}", env, target); + } } Ok(()) diff --git a/src/container.rs b/src/container.rs index ba82152..9adf4be 100644 --- a/src/container.rs +++ b/src/container.rs @@ -12,25 +12,25 @@ use crate::{ pub fn get_filter_service(pool: Db) -> FilterService { let repo = FilterRepo::new(pool); - return FilterService::new(repo); + FilterService::new(repo) } pub fn get_merchant_service(pool: Db) -> MerchantService { let repo = MerchantRepo::new(pool); - return MerchantService::new(repo); + MerchantService::new(repo) } pub fn get_schema_service(pool: Db) -> SchemaService { let repo = SchemaRepo::new(pool); - return SchemaService::new(repo); + SchemaService::new(repo) } pub fn get_page_service(pool: Db) -> FileService { let repo = FileRepo::new(pool); - return FileService::new(repo); + FileService::new(repo) } pub(crate) fn get_job_service(pool: Db) -> JobService { let repo = JobRepo::new(pool); - return JobService::new(repo); + JobService::new(repo) } diff --git a/src/service/file_service.rs b/src/service/file_service.rs index 9085f8f..869b8d9 100644 --- a/src/service/file_service.rs +++ b/src/service/file_service.rs @@ -13,24 +13,20 @@ impl FileService { Self { repo } } - pub fn get_page(&mut self, page_id: &usize) { - let result = self.repo.find_page(page_id); + pub fn get_page(&mut self, page_id: &usize) -> Result<(), Box> { + let page = self.repo.find_page(page_id)?; - match result { - Ok(page) => { - if page.is_empty() { - println!("No page found."); - } else { - page.into_iter().for_each(|page: Page| { - println!("{}", "-".repeat(self.get_width())); - println!("M-ID: {}", page.m_id); - println!("FileName: {}", page.file); - println!("Title: {}", page.title); - }); - println!("{}", "-".repeat(self.get_width())); - } - } - Err(err) => panic!("{}", err), + if page.is_empty() { + println!("No page found."); + } else { + page.into_iter().for_each(|page: Page| { + println!("{}", "-".repeat(self.get_width())); + println!("M-ID: {}", page.m_id); + println!("FileName: {}", page.file); + println!("Title: {}", page.title); + }); + println!("{}", "-".repeat(self.get_width())); } + Ok(()) } } diff --git a/src/service/filter_service.rs b/src/service/filter_service.rs index 8a3679a..97f71a0 100644 --- a/src/service/filter_service.rs +++ b/src/service/filter_service.rs @@ -17,104 +17,106 @@ impl FilterService { Self { repo } } - pub fn get_filter(&mut self, filter_id: &usize, all: bool) { - let result = self.repo.find_by_id(filter_id); + pub fn get_filter( + &mut self, + filter_id: &usize, + all: bool, + ) -> Result<(), Box> { + let filters = self.repo.find_by_id(filter_id)?; - match result { - Ok(filters) => { - if filters.is_empty() { - println!("Filter not found!"); - } else { - filters.into_iter().for_each(|filter: Filter| { - println!("{}", "-".repeat(self.get_width())); - println!("Module ID: {}", filter.filter_module_id); - println!("FileName: {}", filter.file_name); - println!("Description: {}", filter.description); - println!("ModuleNo: {}", filter.filter_module_no); - println!("FilterUser: {}", filter.filter_user); - }); + if filters.is_empty() { + println!("Filter not found!"); + } else { + filters.into_iter().for_each(|filter: Filter| { + println!("{}", "-".repeat(self.get_width())); + println!("Module ID: {}", filter.filter_module_id); + println!("FileName: {}", filter.file_name); + println!("Description: {}", filter.description); + println!("ModuleNo: {}", filter.filter_module_no); + println!("FilterUser: {}", filter.filter_user); + }); - if all { - self.get_filter_configs(filter_id) - } else { - println!("{}", "-".repeat(self.get_width())); + if all { + self.get_filter_configs(filter_id)? + } else { + println!("{}", "-".repeat(self.get_width())); + } + }; + + Ok(()) + } + + pub fn get_filter_configs( + &mut self, + filter_id: &usize, + ) -> Result<(), Box> { + let filter_configs = self.repo.find_filter_configs(filter_id)?; + + if filter_configs.is_empty() { + println!("No filter configs found!"); + } else { + println!("{}", "-".repeat(self.get_width())); + filter_configs + .into_iter() + .for_each(|filter_config: FilterConfig| { + println!( + "{0: <25} | {1: <20} | {2: <20} | {3: <10} | {4: <10}", + filter_config.attribute, + filter_config.value1, + filter_config.value2.unwrap_or("n/a".to_string()), + filter_config.upd_ts, + filter_config.name + ); + }); + println!("{}", "-".repeat(self.get_width())); + }; + + Ok(()) + } + + pub fn get_filter_log( + &mut self, + filter_id: &usize, + all: bool, + ) -> Result<(), Box> { + let filter_log = self.repo.find_filter_log(filter_id)?; + if filter_log.is_empty() { + println!("No filter log found!"); + } else { + println!("{}", "-".repeat(self.get_width())); + filter_log.into_iter().for_each(|filter_log: FilterLog| { + println!( + "TS: {} Error Code: {}", + filter_log.run_ts, filter_log.error_code + ); + match filter_log.error_code.as_str() { + "WARNING" => { + println!("{}", filter_log.error_msg.yellow()); + if all { + println!("{}", filter_log.mysql_error.yellow()); + } + } + "ERROR" => { + println!("{}", filter_log.error_msg.red()); + if all { + println!("{}", filter_log.mysql_error.red()); + } + } + "DEBUG" => { + println!("{}", filter_log.error_msg.green()); + if all { + println!("{}", filter_log.error_msg.green()) + } + } + _ => { + println!("{}", filter_log.error_msg); } } - } - Err(err) => panic!("{}", err), - }; - } - pub fn get_filter_configs(&mut self, filter_id: &usize) { - let result = self.repo.find_filter_configs(filter_id); - - match result { - Ok(filter_configs) => { - if filter_configs.is_empty() { - println!("No filter configs found!"); - } else { - println!("{}", "-".repeat(self.get_width())); - filter_configs - .into_iter() - .for_each(|filter_config: FilterConfig| { - println!( - "{0: <25} | {1: <20} | {2: <20} | {3: <10} | {4: <10}", - filter_config.attribute, - filter_config.value1, - filter_config.value2.unwrap_or("n/a".to_string()), - filter_config.upd_ts, - filter_config.name - ); - }); - println!("{}", "-".repeat(self.get_width())); - } - } - Err(err) => panic!("{}", err), - }; - } - - pub fn get_filter_log(&mut self, filter_id: &usize, all: bool) { - let result = self.repo.find_filter_log(filter_id); - match result { - Ok(filter_log) => { - if filter_log.is_empty() { - println!("No filter log found!"); - } else { - println!("{}", "-".repeat(self.get_width())); - filter_log.into_iter().for_each(|filter_log: FilterLog| { - println!( - "TS: {} Error Code: {}", - filter_log.run_ts, filter_log.error_code - ); - match filter_log.error_code.as_str() { - "WARNING" => { - println!("{}", filter_log.error_msg.yellow()); - if all { - println!("{}", filter_log.mysql_error.yellow()); - } - } - "ERROR" => { - println!("{}", filter_log.error_msg.red()); - if all { - println!("{}", filter_log.mysql_error.red()); - } - } - "DEBUG" => { - println!("{}", filter_log.error_msg.green()); - if all { - println!("{}", filter_log.error_msg.green()) - } - } - _ => { - println!("{}", filter_log.error_msg); - } - } - - println!("{}", "-".repeat(self.get_width())); - }) - } - } - Err(err) => panic!("{}", err), + println!("{}", "-".repeat(self.get_width())); + }) } + + Ok(()) } } diff --git a/src/service/job_service.rs b/src/service/job_service.rs index 0599b4c..f3e5973 100644 --- a/src/service/job_service.rs +++ b/src/service/job_service.rs @@ -16,43 +16,37 @@ impl JobService { Self { repo } } - pub fn get_job_by_id(&mut self, job_id: &usize) { - let result = self.repo.find_by_id(job_id); + pub fn get_job_by_id(&mut self, job_id: &usize) -> Result<(), Box> { + let result = self.repo.find_by_id(job_id)?; - match result { - Ok(job) => { - if job.is_empty() { - println!("Job not found!"); - } else { - job.into_iter().for_each(|job: Job| { - println!("{}", "-".repeat(self.get_width())); - println!("FileName: {}", job.file_name); - println!("Description: {}", job.description); - }); + if result.is_empty() { + println!("Job not found!"); + } else { + result.into_iter().for_each(|job: Job| { + println!("{}", "-".repeat(self.get_width())); + println!("FileName: {}", job.file_name); + println!("Description: {}", job.description); + }); - println!("{}", "-".repeat(self.get_width())); - } - } - Err(err) => panic!("{}", err), + println!("{}", "-".repeat(self.get_width())); } + + Ok(()) } - pub fn get_job_log(&mut self, job_id: &usize) { - let result = self.repo.find_log(job_id); + pub fn get_job_log(&mut self, job_id: &usize) -> Result<(), Box> { + let result = self.repo.find_log(job_id)?; - match result { - Ok(joblog) => { - if joblog.is_empty() { - println!("Job log not found!"); - } else { - joblog.into_iter().for_each(|joblog: JobLog| { - println!("{0: <25} | {1: <20} ", joblog.upd_ts, joblog.content,); - }); + if result.is_empty() { + println!("Job log not found!"); + } else { + result.into_iter().for_each(|joblog: JobLog| { + println!("{0: <25} | {1: <20} ", joblog.upd_ts, joblog.content,); + }); - println!("{}", "-".repeat(self.get_width())); - } - } - Err(err) => panic!("{}", err), + println!("{}", "-".repeat(self.get_width())); } + + Ok(()) } } diff --git a/src/service/merchant_service.rs b/src/service/merchant_service.rs index e01046a..1fb9644 100644 --- a/src/service/merchant_service.rs +++ b/src/service/merchant_service.rs @@ -13,23 +13,20 @@ impl MerchantService { Self { repo } } - pub fn get_merchant(&mut self, search: &str) { - let merchants = self.repo.find_by_name_or_id(search); + pub fn get_merchant(&mut self, search: &str) -> Result<(), Box> { + let merchants = self.repo.find_by_name_or_id(search)?; - match merchants { - Ok(merchants) => { - if merchants.is_empty() { - println!("Merchant not found!"); - } - merchants.into_iter().for_each(|merchant| { - println!("{}", "-".repeat(self.get_width())); - println!("Merchant: {}", merchant.m_name); - println!("M-ID: {}", merchant.m_id); - }); + if merchants.is_empty() { + println!("Merchant not found!"); + } + merchants.into_iter().for_each(|merchant| { + println!("{}", "-".repeat(self.get_width())); + println!("Merchant: {}", merchant.m_name); + println!("M-ID: {}", merchant.m_id); + }); - println!("{}", "-".repeat(self.get_width())); - } - Err(err) => panic!("{}", err), - }; + println!("{}", "-".repeat(self.get_width())); + + Ok(()) } } diff --git a/src/service/mod.rs b/src/service/mod.rs index 7e98c79..a43711f 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,5 +1,6 @@ +pub mod env_service; +pub mod file_service; +pub mod filter_service; +pub mod job_service; pub mod merchant_service; pub mod schema_service; -pub mod filter_service; -pub mod file_service; -pub mod job_service; diff --git a/src/service/schema_service.rs b/src/service/schema_service.rs index 24d5439..b650e6f 100644 --- a/src/service/schema_service.rs +++ b/src/service/schema_service.rs @@ -12,24 +12,21 @@ impl SchemaService { pub fn new(repo: SchemaRepo) -> Self { Self { repo } } - pub fn get_columns(&mut self, column: &str) { - let columns = self.repo.find_by_column(column); + pub fn get_columns(&mut self, column: &str) -> Result<(), Box> { + let columns = self.repo.find_by_column(column)?; - match columns { - Ok(columns) => { - if columns.is_empty() { - println!("No column found!"); - } else { - columns.into_iter().for_each(|column| { - println!("{}", "-".repeat(self.get_width())); - println!("Table name: {}", column.table_name); - println!("Column name: {}", column.column_name); - println!("Type: {}", column.column_type); - }); - println!("{}", "-".repeat(self.get_width())); - } - } - Err(err) => panic!("{err}"), + if columns.is_empty() { + println!("No column found!"); + } else { + columns.into_iter().for_each(|column| { + println!("{}", "-".repeat(self.get_width())); + println!("Table name: {}", column.table_name); + println!("Column name: {}", column.column_name); + println!("Type: {}", column.column_type); + }); + println!("{}", "-".repeat(self.get_width())); } + + Ok(()) } }