Propagate Url::to_file_path() errors instead of panicking (#2771)

* Propagate Url::to_file_path() errors instead of panicking
This commit is contained in:
Bert Belder 2019-08-13 03:52:03 +02:00 committed by GitHub
parent 9bd473d8ac
commit c3afa55751
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,
@ -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();