@@ -4,6 +4,7 @@ use std::{
44 time:: Duration ,
55} ;
66
7+ use anyhow:: anyhow;
78use async_trait:: async_trait;
89use bytes:: Bytes ;
910use graph_derive:: CheapClone ;
@@ -13,7 +14,9 @@ use slog::{warn, Logger};
1314
1415use 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 ) ]
1922enum Cache {
@@ -37,27 +40,26 @@ fn log_err(logger: &Logger, e: &object_store::Error, log_not_found: bool) {
3740}
3841
3942impl 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
121123impl 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 >
0 commit comments