Skip to content

Commit 9444d38

Browse files
committed
graph: Don't panic if the IPFS cache is misconfigured
1 parent bf78bc4 commit 9444d38

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

graph/src/ipfs/cache.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
time::Duration,
55
};
66

7+
use anyhow::anyhow;
78
use async_trait::async_trait;
89
use bytes::Bytes;
910
use graph_derive::CheapClone;
@@ -13,7 +14,9 @@ use slog::{warn, Logger};
1314

1415
use crate::{env::ENV_VARS, prelude::CheapClone};
1516

16-
use super::{ContentPath, IpfsClient, IpfsRequest, IpfsResponse, IpfsResult, RetryPolicy};
17+
use super::{
18+
ContentPath, IpfsClient, IpfsError, IpfsRequest, IpfsResponse, IpfsResult, RetryPolicy,
19+
};
1720

1821
#[derive(Clone, CheapClone)]
1922
enum Cache {
@@ -37,27 +40,26 @@ fn log_err(logger: &Logger, e: &object_store::Error, log_not_found: bool) {
3740
}
3841

3942
impl Cache {
40-
fn new(capacity: usize, max_entry_size: usize, path: Option<PathBuf>) -> Self {
43+
fn new(capacity: usize, max_entry_size: usize, path: Option<PathBuf>) -> IpfsResult<Self> {
4144
match path {
4245
Some(path) => {
43-
let fs = match LocalFileSystem::new_with_prefix(&path) {
44-
Err(e) => {
45-
panic!(
46+
let fs = LocalFileSystem::new_with_prefix(&path).map_err(|e| {
47+
IpfsError::InvalidCacheConfig {
48+
source: anyhow!(
4649
"Failed to create IPFS file based cache at {}: {}",
4750
path.display(),
4851
e
49-
);
52+
),
5053
}
51-
Ok(fs) => fs,
52-
};
53-
Cache::Disk {
54+
})?;
55+
Ok(Cache::Disk {
5456
store: Arc::new(fs),
55-
}
57+
})
5658
}
57-
None => Self::Memory {
59+
None => Ok(Self::Memory {
5860
cache: Arc::new(Mutex::new(LruCache::with_capacity(capacity))),
5961
max_entry_size,
60-
},
62+
}),
6163
}
6264
}
6365

@@ -119,15 +121,15 @@ pub struct CachingClient {
119121
}
120122

121123
impl CachingClient {
122-
pub fn new(client: Arc<dyn IpfsClient>) -> Self {
124+
pub fn new(client: Arc<dyn IpfsClient>) -> IpfsResult<Self> {
123125
let env = &ENV_VARS.mappings;
124126

125127
let cache = Cache::new(
126128
env.max_ipfs_cache_size as usize,
127129
env.max_ipfs_cache_file_size,
128130
env.ipfs_cache_location.clone(),
129-
);
130-
CachingClient { client, cache }
131+
)?;
132+
Ok(CachingClient { client, cache })
131133
}
132134

133135
async fn with_cache<F>(&self, path: &ContentPath, f: F) -> IpfsResult<Bytes>

graph/src/ipfs/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub enum IpfsError {
4949

5050
#[error(transparent)]
5151
RequestFailed(RequestError),
52+
53+
#[error("Invalid cache configuration: {source}")]
54+
InvalidCacheConfig { source: anyhow::Error },
5255
}
5356

5457
#[derive(Debug, Error)]
@@ -91,6 +94,7 @@ impl IpfsError {
9194
Self::RequestTimeout { .. } => false,
9295
Self::DeterministicFailure { .. } => true,
9396
Self::RequestFailed(_) => false,
97+
Self::InvalidCacheConfig { .. } => true,
9498
}
9599
}
96100
}

graph/src/ipfs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
SafeDisplay(server_address)
6363
);
6464

65-
let client = CachingClient::new(use_first_valid_api(server_address, logger).await?);
65+
let client = CachingClient::new(use_first_valid_api(server_address, logger).await?)?;
6666
clients.push(Arc::new(client));
6767
}
6868

0 commit comments

Comments
 (0)