From 51245d34f172f9025699e8d9653be62625872a49 Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Tue, 7 Sep 2021 19:41:14 +0100 Subject: [PATCH] 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 --- src/database.rs | 5 +++++ src/main.rs | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/database.rs b/src/database.rs index 6ea0abdc..5fb6de46 100644 --- a/src/database.rs +++ b/src/database.rs @@ -198,6 +198,11 @@ impl Database { pub async fn load_or_create(config: &Config) -> Result>> { 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)?; if config.max_request_size < 1024 { diff --git a/src/main.rs b/src/main.rs index 2ca49e25..06409eeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -199,16 +199,27 @@ async fn main() { std::env::set_var("RUST_LOG", "warn"); - let config = raw_config - .extract::() - .expect("It looks like your config is invalid. Please take a look at the error"); + let config = match raw_config.extract::() { + Ok(s) => s, + 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 { config.warn_deprecated(); - let db = Database::load_or_create(&config) - .await - .expect("config is valid"); + let db = match Database::load_or_create(&config).await { + Ok(db) => db, + 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)) .ignite()