Skip to content

Commit

Permalink
fix: add context when failing to parse a URL (#29)
Browse files Browse the repository at this point in the history
The error was missing what URL was failing.
  • Loading branch information
dsherret authored Jan 15, 2024
1 parent b29207c commit c781e4d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,12 @@ impl PathOrPattern {
|| path.starts_with("https://")
|| path.starts_with("file://")
{
let url = Url::parse(path)?;
let url =
Url::parse(path).with_context(|| format!("Invalid URL '{}'", path))?;
if url.scheme() == "file" {
let path = url
.to_file_path()
.map_err(|_| anyhow::anyhow!("Invalid file URL: \"{}\"", path))?;
.map_err(|_| anyhow::anyhow!("Invalid file URL '{}'", path))?;
return Ok(Self::Path(path));
} else {
return Ok(Self::RemoteUrl(url));
Expand Down Expand Up @@ -570,6 +571,16 @@ mod test {
assert_eq!(pattern.matches_path(&cwd.join("dir/foo.ts")), false);
assert_eq!(pattern.matches_path(&cwd.join("foo.js")), false);
}
// error for invalid url
{
let err = PathOrPattern::from_relative(&cwd, "https://raw.githubusercontent.com%2Fdyedgreen%2Fdeno-sqlite%2Frework_api%2Fmod.ts").unwrap_err();
assert_eq!(format!("{:#}", err), "Invalid URL 'https://raw.githubusercontent.com%2Fdyedgreen%2Fdeno-sqlite%2Frework_api%2Fmod.ts': invalid domain character");
}
// error for invalid file url
if cfg!(windows) {
let err = PathOrPattern::from_relative(&cwd, "file:///raw.githubusercontent.com%2Fdyedgreen%2Fdeno-sqlite%2Frework_api%2Fmod.ts").unwrap_err();
assert_eq!(format!("{:#}", err), "Invalid file URL 'file:///raw.githubusercontent.com%2Fdyedgreen%2Fdeno-sqlite%2Frework_api%2Fmod.ts'");
}
}

#[test]
Expand Down

0 comments on commit c781e4d

Please sign in to comment.