Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add context when failing to parse a URL #29

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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