Skip to content

Commit

Permalink
Propagate Url::to_file_path() errors instead of panicking (#2771)
Browse files Browse the repository at this point in the history
* Propagate Url::to_file_path() errors instead of panicking
  • Loading branch information
piscisaureus authored Aug 13, 2019
1 parent 9bd473d commit c3afa55
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -716,6 +721,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();
Expand Down

0 comments on commit c3afa55

Please sign in to comment.