error handling improved

main
Mathias Rothenhaeusler 2023-06-13 12:48:41 +02:00
parent 5f840f2235
commit 661856a271
3 changed files with 44 additions and 41 deletions

2
Cargo.lock generated
View File

@ -1280,7 +1280,7 @@ dependencies = [
[[package]]
name = "rcc"
version = "0.1.0"
version = "1.0.1"
dependencies = [
"clap",
"colored",

View File

@ -36,7 +36,7 @@ impl Db {
}
}
pub fn get_connection(&self) -> PooledConn {
pub fn get_connection(&self) -> Result<PooledConn, mysql::Error> {
let url = sprintf!(
"mysql://%s:%s@%s:%s/%s",
self.user,
@ -48,12 +48,9 @@ impl Db {
let pool = match Pool::new(Opts::from_url(&url).unwrap()) {
Ok(p) => p,
Err(e) => panic!("Cannot initialize pool: {e}"),
};
let conn: PooledConn = match pool.get_conn() {
Ok(db) => db,
Err(e) => panic!("Cannot connect to DB: {e}"),
Err(e) => return Err(e),
};
let conn: Result<PooledConn, mysql::Error> = pool.get_conn() ;
conn
}

View File

@ -18,11 +18,13 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();
let env = args.env.unwrap_or("local".to_string());
let db = Db::initialize(env);
let mut conn: PooledConn = db.get_connection();
let conn_res: Result<PooledConn, mysql::Error> = db.get_connection();
match args.mode {
match conn_res {
Ok(mut conn) => match args.mode {
Commands::Merch { search } => {
let mut merchant_service: MerchantService = container::get_merchant_service(&mut conn);
let mut merchant_service: MerchantService =
container::get_merchant_service(&mut conn);
merchant_service.get_merchant(&search.unwrap());
}
Commands::Schema { search } => {
@ -56,6 +58,10 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
job_service.get_job_by_id(&job_id);
}
}
},
Err(e) => {
println!("Error connecting DB: {e}");
}
}
Ok(())