@@ -4,6 +4,7 @@ use std::{
4
4
time:: Duration ,
5
5
} ;
6
6
7
+ use anyhow:: anyhow;
7
8
use async_trait:: async_trait;
8
9
use bytes:: Bytes ;
9
10
use graph_derive:: CheapClone ;
@@ -13,7 +14,9 @@ use slog::{warn, Logger};
13
14
14
15
use crate :: { env:: ENV_VARS , prelude:: CheapClone } ;
15
16
16
- use super :: { ContentPath , IpfsClient , IpfsRequest , IpfsResponse , IpfsResult , RetryPolicy } ;
17
+ use super :: {
18
+ ContentPath , IpfsClient , IpfsError , IpfsRequest , IpfsResponse , IpfsResult , RetryPolicy ,
19
+ } ;
17
20
18
21
#[ derive( Clone , CheapClone ) ]
19
22
enum Cache {
@@ -37,27 +40,26 @@ fn log_err(logger: &Logger, e: &object_store::Error, log_not_found: bool) {
37
40
}
38
41
39
42
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 > {
41
44
match path {
42
45
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 ! (
46
49
"Failed to create IPFS file based cache at {}: {}" ,
47
50
path. display( ) ,
48
51
e
49
- ) ;
52
+ ) ,
50
53
}
51
- Ok ( fs) => fs,
52
- } ;
53
- Cache :: Disk {
54
+ } ) ?;
55
+ Ok ( Cache :: Disk {
54
56
store : Arc :: new ( fs) ,
55
- }
57
+ } )
56
58
}
57
- None => Self :: Memory {
59
+ None => Ok ( Self :: Memory {
58
60
cache : Arc :: new ( Mutex :: new ( LruCache :: with_capacity ( capacity) ) ) ,
59
61
max_entry_size,
60
- } ,
62
+ } ) ,
61
63
}
62
64
}
63
65
@@ -119,15 +121,15 @@ pub struct CachingClient {
119
121
}
120
122
121
123
impl CachingClient {
122
- pub fn new ( client : Arc < dyn IpfsClient > ) -> Self {
124
+ pub fn new ( client : Arc < dyn IpfsClient > ) -> IpfsResult < Self > {
123
125
let env = & ENV_VARS . mappings ;
124
126
125
127
let cache = Cache :: new (
126
128
env. max_ipfs_cache_size as usize ,
127
129
env. max_ipfs_cache_file_size ,
128
130
env. ipfs_cache_location . clone ( ) ,
129
- ) ;
130
- CachingClient { client, cache }
131
+ ) ? ;
132
+ Ok ( CachingClient { client, cache } )
131
133
}
132
134
133
135
async fn with_cache < F > ( & self , path : & ContentPath , f : F ) -> IpfsResult < Bytes >
0 commit comments