summaryrefslogtreecommitdiff
path: root/cli/file_fetcher.rs
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2019-08-13 03:52:03 +0200
committerGitHub <noreply@github.com>2019-08-13 03:52:03 +0200
commitc3afa557515c64610b23ee460f8c6251de421f1a (patch)
tree60ee00da5c6fab261174c3621fa1247451db5f30 /cli/file_fetcher.rs
parent9bd473d8ac9228e483bd26028dbe7ba88e971c08 (diff)
Propagate Url::to_file_path() errors instead of panicking (#2771)
* Propagate Url::to_file_path() errors instead of panicking
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r--cli/file_fetcher.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index 3e3c29002..0e69560ef 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -213,7 +213,12 @@ impl SourceFileFetcher {
self: &Self,
module_url: &Url,
) -> Result<SourceFile, ErrBox> {
- let filepath = module_url.to_file_path().expect("File URL expected");
+ let filepath = module_url.to_file_path().map_err(|()| {
+ ErrBox::from(DenoError::new(
+ ErrorKind::InvalidPath,
+ "File URL contains invalid path".to_owned(),
+ ))
+ })?;
let source_code = match fs::read(filepath.clone()) {
Ok(c) => c,
@@ -717,6 +722,20 @@ mod tests {
}
#[test]
+ fn test_fetch_local_file_no_panic() {
+ let (_temp_dir, fetcher) = test_setup();
+ if cfg!(windows) {
+ // Should fail: missing drive letter.
+ let u = Url::parse("file:///etc/passwd").unwrap();
+ fetcher.fetch_local_file(&u).unwrap_err();
+ } else {
+ // Should fail: local network paths are not supported on unix.
+ let u = Url::parse("file://server/etc/passwd").unwrap();
+ fetcher.fetch_local_file(&u).unwrap_err();
+ }
+ }
+
+ #[test]
fn test_get_source_code_1() {
let (temp_dir, fetcher) = test_setup();
// http_util::fetch_sync_string requires tokio