Skip to content

Commit afd924c

Browse files
committed
fix: remove unwrap on DatabaseState::new
1 parent 80b32c3 commit afd924c

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/wasm/src/database/mod.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod utils;
33

44
use anyhow::{anyhow, Result};
55
use once_cell::sync::Lazy;
6+
use sentry::integrations::anyhow::capture_anyhow;
67
use serde::Deserialize;
78
use std::{
89
cmp::Ordering,
@@ -50,13 +51,14 @@ pub const BUNDLED_FOLDER_NAME: &str = ".\\NavigationData";
5051

5152
/// The global exported database state
5253
pub static DATABASE_STATE: Lazy<Mutex<DatabaseState>> =
53-
Lazy::new(|| Mutex::new(DatabaseState::new().unwrap())); // SAFETY: the only way this function can return an error is if there is an IO error which is impossible unless the user has messed up work folder permissions (which would make interface not work anyways)
54+
Lazy::new(|| Mutex::new(DatabaseState::new()));
5455

5556
/// Find the bundled navigation data distribution
5657
fn get_bundled_db() -> Result<Option<DatabaseDistributionInfo>> {
57-
let bundled_entries = read_dir(BUNDLED_FOLDER_NAME)?
58-
.filter_map(Result::ok)
59-
.collect::<Vec<_>>();
58+
let bundled_entries = match read_dir(BUNDLED_FOLDER_NAME) {
59+
Ok(dir) => dir.filter_map(Result::ok).collect::<Vec<_>>(),
60+
Err(_) => return Ok(None),
61+
};
6062

6163
// Try finding cycle.json
6264
let Some(cycle_file_name) = bundled_entries
@@ -138,11 +140,24 @@ pub struct DatabaseState {
138140

139141
impl DatabaseState {
140142
/// Create a database state, intended to only be instantiated once (held in the DATABASE_STATE static)
141-
///
142-
/// This searches for the best DB to use by comparing the cycle and revision of both the downloaded (in work folder) and bundled navigation data.
143-
fn new() -> Result<Self> {
143+
fn new() -> Self {
144144
// Start out with a fresh instance
145145
let mut instance = Self::default();
146+
match instance.try_load_db() {
147+
Ok(()) => {}
148+
Err(e) => {
149+
capture_anyhow(&e);
150+
println!("[NAVIGRAPH]: Error trying to load DB: {e}");
151+
}
152+
}
153+
154+
instance
155+
}
156+
157+
/// Try to load a database (either bundled or downloaded)
158+
///
159+
/// This searches for the best DB to use by comparing the cycle and revision of both the downloaded (in work folder) and bundled navigation data.
160+
fn try_load_db(&mut self) -> Result<()> {
146161
// Get distribution info of both bundled and downloaded DBs, if they exist
147162
let bundled_distribution = get_bundled_db()?;
148163
let downloaded_distribution =
@@ -182,7 +197,7 @@ impl DatabaseState {
182197

183198
// If we somehow don't have a cycle in bundled or downloaded, return an empty instance
184199
let Some(latest) = latest else {
185-
return Ok(instance);
200+
return Ok(());
186201
};
187202

188203
// Ensure parent folder exists (ignore the result as it will return an error if it already exists)
@@ -197,9 +212,9 @@ impl DatabaseState {
197212
}
198213

199214
// The only way this can fail (since we know now that the path is valid) is if the file is corrupt, in which case we should report to sentry
200-
instance.open_connection()?;
215+
self.open_connection()?;
201216

202-
Ok(instance)
217+
return Ok(());
203218
}
204219

205220
fn get_database(&self) -> Result<&Connection> {

0 commit comments

Comments
 (0)