From dabe88f854b7cc04518c5ff75c55fa437360a91f Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 7 May 2020 21:32:57 +0900 Subject: fix(deno_dir): better error message (#5120) Add better error messages when a cache subdirectory in `DENO_DIR` cannot be created. --- cli/disk_cache.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'cli/disk_cache.rs') 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>( 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); } -- cgit v1.2.3