fix(database): handle errors in config parsin or database creation

Showing the user a backtrace can be pretty confusing, so just printing
a nice error message makes errors easier to understand for end users.

fixes #121
next
Rasmus Thomsen 2021-09-07 19:41:14 +01:00
parent 0737bc021f
commit 51245d34f1
No known key found for this signature in database
GPG Key ID: 1115241518154EA6
2 changed files with 22 additions and 6 deletions

View File

@ -198,6 +198,11 @@ impl Database {
pub async fn load_or_create(config: &Config) -> Result<Arc<TokioRwLock<Self>>> { pub async fn load_or_create(config: &Config) -> Result<Arc<TokioRwLock<Self>>> {
Self::check_sled_or_sqlite_db(&config)?; Self::check_sled_or_sqlite_db(&config)?;
if !Path::new(&config.database_path).exists() {
std::fs::create_dir_all(&config.database_path)
.map_err(|_| Error::BadConfig("Database folder doesn't exists and couldn't be created (e.g. due to missing permissions). Please create the database folder yourself."))?;
}
let builder = Engine::open(&config)?; let builder = Engine::open(&config)?;
if config.max_request_size < 1024 { if config.max_request_size < 1024 {

View File

@ -199,16 +199,27 @@ async fn main() {
std::env::set_var("RUST_LOG", "warn"); std::env::set_var("RUST_LOG", "warn");
let config = raw_config let config = match raw_config.extract::<Config>() {
.extract::<Config>() Ok(s) => s,
.expect("It looks like your config is invalid. Please take a look at the error"); Err(e) => {
eprintln!("It looks like your config is invalid. The following error occured while parsing it: {}", e);
std::process::exit(1);
}
};
let start = async { let start = async {
config.warn_deprecated(); config.warn_deprecated();
let db = Database::load_or_create(&config) let db = match Database::load_or_create(&config).await {
.await Ok(db) => db,
.expect("config is valid"); Err(e) => {
eprintln!(
"The database couldn't be loaded or created. The following error occured: {}",
e
);
std::process::exit(1);
}
};
let rocket = setup_rocket(raw_config, Arc::clone(&db)) let rocket = setup_rocket(raw_config, Arc::clone(&db))
.ignite() .ignite()