@@ -3,6 +3,7 @@ mod utils;
3
3
4
4
use anyhow:: { anyhow, Result } ;
5
5
use once_cell:: sync:: Lazy ;
6
+ use sentry:: integrations:: anyhow:: capture_anyhow;
6
7
use serde:: Deserialize ;
7
8
use std:: {
8
9
cmp:: Ordering ,
@@ -50,13 +51,14 @@ pub const BUNDLED_FOLDER_NAME: &str = ".\\NavigationData";
50
51
51
52
/// The global exported database state
52
53
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 ( ) ) ) ;
54
55
55
56
/// Find the bundled navigation data distribution
56
57
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
+ } ;
60
62
61
63
// Try finding cycle.json
62
64
let Some ( cycle_file_name) = bundled_entries
@@ -138,11 +140,24 @@ pub struct DatabaseState {
138
140
139
141
impl DatabaseState {
140
142
/// 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 {
144
144
// Start out with a fresh instance
145
145
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 < ( ) > {
146
161
// Get distribution info of both bundled and downloaded DBs, if they exist
147
162
let bundled_distribution = get_bundled_db ( ) ?;
148
163
let downloaded_distribution =
@@ -182,7 +197,7 @@ impl DatabaseState {
182
197
183
198
// If we somehow don't have a cycle in bundled or downloaded, return an empty instance
184
199
let Some ( latest) = latest else {
185
- return Ok ( instance ) ;
200
+ return Ok ( ( ) ) ;
186
201
} ;
187
202
188
203
// Ensure parent folder exists (ignore the result as it will return an error if it already exists)
@@ -197,9 +212,9 @@ impl DatabaseState {
197
212
}
198
213
199
214
// 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 ( ) ?;
201
216
202
- Ok ( instance )
217
+ return Ok ( ( ) ) ;
203
218
}
204
219
205
220
fn get_database ( & self ) -> Result < & Connection > {
0 commit comments