From 997e8b419f2985a332eec52857216382ca6cd8a4 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 13 Sep 2023 11:06:40 -0700 Subject: [PATCH] handle setup errors more gracefully --- doc/todo.md | 2 +- src-tauri/src/app.rs | 18 +++++++++++++++--- src-tauri/src/errors.rs | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/doc/todo.md b/doc/todo.md index 2576af6..f032674 100644 --- a/doc/todo.md +++ b/doc/todo.md @@ -2,7 +2,7 @@ * Switch to "process" provider for AWS credentials (much less hacky) * Session timeout (plain duration, or activity-based?) -* Fix rehide behavior when new request comes in while old one is still being resolved +* ~Fix rehide behavior when new request comes in while old one is still being resolved~ * Additional hotkey configuration (approve/deny at the very least) * Logging * Icon diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs index 812eb4c..e4a2a67 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/app.rs @@ -75,15 +75,27 @@ pub async fn connect_db() -> Result { async fn setup(app: &mut App) -> Result<(), Box> { APP.set(app.handle()).unwrap(); - let is_first_launch = config::get_or_create_db_path()?.exists(); + // get_or_create_db_path doesn't create the actual db file, just the directory + let is_first_launch = !config::get_or_create_db_path()?.exists(); let pool = connect_db().await?; - let conf = AppConfig::load(&pool).await?; + + let conf = match AppConfig::load(&pool).await { + Ok(c) => c, + Err(SetupError::ConfigParseError(_)) => { + Err("Could not load configuration from database. Reverting to defaults.") + .error_popup("Setup error"); + AppConfig::default() + }, + err => err?, + }; + let session = Session::load(&pool).await?; let srv = Server::new(conf.listen_addr, conf.listen_port, app.handle()).await?; config::set_auto_launch(conf.start_on_login)?; - config::register_hotkeys(&conf.hotkeys)?; + config::register_hotkeys(&conf.hotkeys).error_popup("Setup error"); + // if session is empty, this is probably the first launch, so don't autohide if !conf.start_minimized || is_first_launch { app.get_window("main") diff --git a/src-tauri/src/errors.rs b/src-tauri/src/errors.rs index 5096154..266f3fd 100644 --- a/src-tauri/src/errors.rs +++ b/src-tauri/src/errors.rs @@ -24,7 +24,7 @@ pub trait ErrorPopup { fn error_popup(self, title: &str); } -impl ErrorPopup for Result<(), E> { +impl ErrorPopup for Result<(), E> { fn error_popup(self, title: &str) { if let Err(e) = self { let (tx, rx) = mpsc::channel();