Skip to content

Commit 637b1d5

Browse files
authored
fix(ext/cache): don't panic when creating cache (denoland#26780)
1 parent bf82c66 commit 637b1d5

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

ext/cache/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ pub enum CacheError {
3333
}
3434

3535
#[derive(Clone)]
36-
pub struct CreateCache<C: Cache + 'static>(pub Arc<dyn Fn() -> C>);
36+
pub struct CreateCache<C: Cache + 'static>(
37+
pub Arc<dyn Fn() -> Result<C, CacheError>>,
38+
);
3739

3840
deno_core::extension!(deno_cache,
3941
deps = [ deno_webidl, deno_web, deno_url, deno_fetch ],
@@ -231,7 +233,7 @@ where
231233
if let Some(cache) = state.try_borrow::<CA>() {
232234
Ok(cache.clone())
233235
} else if let Some(create_cache) = state.try_borrow::<CreateCache<CA>>() {
234-
let cache = create_cache.0();
236+
let cache = create_cache.0()?;
235237
state.put(cache);
236238
Ok(state.borrow::<CA>().clone())
237239
} else {

ext/cache/sqlite.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct SqliteBackedCache {
4242
}
4343

4444
impl SqliteBackedCache {
45-
pub fn new(cache_storage_dir: PathBuf) -> Self {
45+
pub fn new(cache_storage_dir: PathBuf) -> Result<Self, CacheError> {
4646
{
4747
std::fs::create_dir_all(&cache_storage_dir)
4848
.expect("failed to create cache dir");
@@ -57,18 +57,14 @@ impl SqliteBackedCache {
5757
PRAGMA synchronous=NORMAL;
5858
PRAGMA optimize;
5959
";
60-
connection
61-
.execute_batch(initial_pragmas)
62-
.expect("failed to execute pragmas");
63-
connection
64-
.execute(
65-
"CREATE TABLE IF NOT EXISTS cache_storage (
60+
connection.execute_batch(initial_pragmas)?;
61+
connection.execute(
62+
"CREATE TABLE IF NOT EXISTS cache_storage (
6663
id INTEGER PRIMARY KEY,
6764
cache_name TEXT NOT NULL UNIQUE
6865
)",
69-
(),
70-
)
71-
.expect("failed to create cache_storage table");
66+
(),
67+
)?;
7268
connection
7369
.execute(
7470
"CREATE TABLE IF NOT EXISTS request_response_list (
@@ -86,12 +82,11 @@ impl SqliteBackedCache {
8682
UNIQUE (cache_id, request_url)
8783
)",
8884
(),
89-
)
90-
.expect("failed to create request_response_list table");
91-
SqliteBackedCache {
85+
)?;
86+
Ok(SqliteBackedCache {
9287
connection: Arc::new(Mutex::new(connection)),
9388
cache_storage_dir,
94-
}
89+
})
9590
}
9691
}
9792
}

0 commit comments

Comments
 (0)