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/disk_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/disk_cache.rs')
-rw-r--r-- | cli/disk_cache.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/cli/disk_cache.rs b/cli/disk_cache.rs index e458bb135..f486e88af 100644 --- a/cli/disk_cache.rs +++ b/cli/disk_cache.rs @@ -1,6 +1,7 @@ use crate::fs as deno_fs; use std::ffi::OsStr; use std::fs; +use std::io; use std::path::Component; use std::path::Path; use std::path::PathBuf; @@ -22,14 +23,24 @@ fn with_io_context<T: AsRef<str>>( impl DiskCache { pub fn new(location: &Path) -> Self { - if !&location.is_dir() { - fs::create_dir_all(&location).ok(); - } 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 TypeScript compiler cache location: {:?}\nCheck the permission of the directory.", + self.location + )) + }) + } + pub fn get_cache_filename(&self, url: &Url) -> PathBuf { let mut out = PathBuf::new(); @@ -140,7 +151,8 @@ mod tests { let cache_location = TempDir::new().unwrap(); let mut cache_path = cache_location.path().to_owned(); cache_path.push("foo"); - DiskCache::new(&cache_path); + let cache = DiskCache::new(&cache_path); + cache.ensure_location().expect("Testing expect:"); assert!(cache_path.is_dir()); } @@ -151,7 +163,8 @@ mod tests { assert!(fs::remove_dir(&cache_location).is_ok()); cache_location.push("foo"); assert_eq!(cache_location.is_dir(), false); - DiskCache::new(&cache_location); + let cache = DiskCache::new(&cache_location); + cache.ensure_location().expect("Testing expect:"); assert_eq!(cache_location.is_dir(), true); } |