improve error handling part 3
parent
5642ab4717
commit
f84387d238
|
@ -1,30 +1,43 @@
|
||||||
|
use core::fmt;
|
||||||
use mysql::{Opts, Pool, PooledConn};
|
use mysql::{Opts, Pool, PooledConn};
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
|
error::Error,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use directories::BaseDirs;
|
use directories::BaseDirs;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct InitError(String);
|
||||||
|
|
||||||
|
impl fmt::Display for InitError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for InitError {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Db {
|
pub struct Db {
|
||||||
pool: Arc<Mutex<Pool>>,
|
pool: Arc<Mutex<Pool>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Db {
|
impl Db {
|
||||||
pub fn initialize(env: String) -> Result<Self, mysql::Error> {
|
pub fn initialize(env: String) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let base_dir: BaseDirs = match BaseDirs::new() {
|
let base_dir: BaseDirs = BaseDirs::new()
|
||||||
Some(dirs) => dirs,
|
.ok_or_else(|| Box::new(InitError("No config folder found.".to_string())))?;
|
||||||
None => panic!("No config folder found."),
|
|
||||||
};
|
|
||||||
|
|
||||||
let config_file_path: PathBuf = base_dir.config_dir().join("rcc/").join(env);
|
let config_file_path: PathBuf = base_dir.config_dir().join("rcc/").join(env);
|
||||||
|
|
||||||
match dotenv::from_path(config_file_path.as_path()) {
|
dotenv::from_path(config_file_path.as_path()).map_err(|_| {
|
||||||
Ok(env) => env,
|
Box::new(InitError(format!(
|
||||||
Err(_) => panic!("Could not load .env file {:?}", config_file_path.as_path()),
|
"Failed to load dotenv file '{}'",
|
||||||
};
|
config_file_path.display()
|
||||||
|
)))
|
||||||
|
})?;
|
||||||
|
|
||||||
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").unwrap();
|
||||||
|
|
|
@ -15,8 +15,7 @@ impl FileRepo {
|
||||||
pub fn find_page(&mut self, file_id: &usize) -> Result<Vec<Page>, mysql::Error> {
|
pub fn find_page(&mut self, file_id: &usize) -> Result<Vec<Page>, mysql::Error> {
|
||||||
let mut connection: mysql::PooledConn = self.db_pool.get_connection()?;
|
let mut connection: mysql::PooledConn = self.db_pool.get_connection()?;
|
||||||
let stat = connection
|
let stat = connection
|
||||||
.prep("SELECT m_id, description, file FROM global_data.effi_file_mapping WHERE file_id = :file_id")
|
.prep("SELECT m_id, description, file FROM global_data.effi_file_mapping WHERE file_id = :file_id")?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
connection.exec_map(
|
connection.exec_map(
|
||||||
stat,
|
stat,
|
||||||
|
|
Loading…
Reference in New Issue