diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2020-05-07 21:32:57 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-07 14:32:57 +0200 |
commit | dabe88f854b7cc04518c5ff75c55fa437360a91f (patch) | |
tree | 9505e8cce910ab80162e2e7ab7f145334b55860c /cli/http_cache.rs | |
parent | 761b7efb3b8a140caad12803619a2e8a535cc178 (diff) |
fix(deno_dir): better error message (#5120)
Add better error messages when a cache subdirectory in
`DENO_DIR` cannot be created.
Diffstat (limited to 'cli/http_cache.rs')
-rw-r--r-- | cli/http_cache.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/cli/http_cache.rs b/cli/http_cache.rs index 1f507316c..2771cb763 100644 --- a/cli/http_cache.rs +++ b/cli/http_cache.rs @@ -11,6 +11,7 @@ use serde::Serialize; use serde_derive::Deserialize; use std::fs; use std::fs::File; +use std::io; use std::path::Path; use std::path::PathBuf; use url::Url; @@ -101,12 +102,26 @@ impl Metadata { } impl HttpCache { - /// Returns error if unable to create directory - /// at specified location. - pub fn new(location: &Path) -> Result<Self, ErrBox> { - fs::create_dir_all(&location)?; - Ok(Self { + /// Returns a new instance. + pub fn new(location: &Path) -> Self { + Self { location: location.to_owned(), + } + } + + /// Ensures the location of the cache. + pub fn ensure_location(&self) -> io::Result<()> { + if self.location.is_dir() { + return Ok(()); + } + fs::create_dir_all(&self.location).map_err(|e| { + io::Error::new( + e.kind(), + format!( + "Could not create remote modules cache location: {:?}\nCheck the permission of the directory.", + self.location + ), + ) }) } @@ -169,15 +184,15 @@ mod tests { let dir = TempDir::new().unwrap(); let mut cache_path = dir.path().to_owned(); cache_path.push("foobar"); - let r = HttpCache::new(&cache_path); - assert!(r.is_ok()); + let cache = HttpCache::new(&cache_path); + assert!(cache.ensure_location().is_ok()); assert!(cache_path.is_dir()); } #[test] fn test_get_set() { let dir = TempDir::new().unwrap(); - let cache = HttpCache::new(dir.path()).unwrap(); + let cache = HttpCache::new(dir.path()); let url = Url::parse("https://deno.land/x/welcome.ts").unwrap(); let mut headers = HashMap::new(); headers.insert( |