diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/cache/lib.rs | 6 | ||||
-rw-r--r-- | ext/cache/sqlite.rs | 23 |
2 files changed, 13 insertions, 16 deletions
diff --git a/ext/cache/lib.rs b/ext/cache/lib.rs index b9cc5427c..524d4cea0 100644 --- a/ext/cache/lib.rs +++ b/ext/cache/lib.rs @@ -33,7 +33,9 @@ pub enum CacheError { } #[derive(Clone)] -pub struct CreateCache<C: Cache + 'static>(pub Arc<dyn Fn() -> C>); +pub struct CreateCache<C: Cache + 'static>( + pub Arc<dyn Fn() -> Result<C, CacheError>>, +); deno_core::extension!(deno_cache, deps = [ deno_webidl, deno_web, deno_url, deno_fetch ], @@ -231,7 +233,7 @@ where if let Some(cache) = state.try_borrow::<CA>() { Ok(cache.clone()) } else if let Some(create_cache) = state.try_borrow::<CreateCache<CA>>() { - let cache = create_cache.0(); + let cache = create_cache.0()?; state.put(cache); Ok(state.borrow::<CA>().clone()) } else { diff --git a/ext/cache/sqlite.rs b/ext/cache/sqlite.rs index e4991c32f..6efceda11 100644 --- a/ext/cache/sqlite.rs +++ b/ext/cache/sqlite.rs @@ -42,7 +42,7 @@ pub struct SqliteBackedCache { } impl SqliteBackedCache { - pub fn new(cache_storage_dir: PathBuf) -> Self { + pub fn new(cache_storage_dir: PathBuf) -> Result<Self, CacheError> { { std::fs::create_dir_all(&cache_storage_dir) .expect("failed to create cache dir"); @@ -57,18 +57,14 @@ impl SqliteBackedCache { PRAGMA synchronous=NORMAL; PRAGMA optimize; "; - connection - .execute_batch(initial_pragmas) - .expect("failed to execute pragmas"); - connection - .execute( - "CREATE TABLE IF NOT EXISTS cache_storage ( + connection.execute_batch(initial_pragmas)?; + connection.execute( + "CREATE TABLE IF NOT EXISTS cache_storage ( id INTEGER PRIMARY KEY, cache_name TEXT NOT NULL UNIQUE )", - (), - ) - .expect("failed to create cache_storage table"); + (), + )?; connection .execute( "CREATE TABLE IF NOT EXISTS request_response_list ( @@ -86,12 +82,11 @@ impl SqliteBackedCache { UNIQUE (cache_id, request_url) )", (), - ) - .expect("failed to create request_response_list table"); - SqliteBackedCache { + )?; + Ok(SqliteBackedCache { connection: Arc::new(Mutex::new(connection)), cache_storage_dir, - } + }) } } } |