Skip to content

Commit ad94f27

Browse files
committed
Show more precise error messages for invalid URLs
Be sure to include the field name in the error message. Closes rust-lang/cargo#1490
1 parent 71b962a commit ad94f27

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/krate.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ impl Crate {
108108
let mut license = license.as_ref().map(|s| &s[..]);
109109
let license_file = license_file.as_ref().map(|s| &s[..]);
110110
let keywords = keywords.connect(",");
111-
try!(validate_url(homepage));
112-
try!(validate_url(documentation));
113-
try!(validate_url(repository));
111+
try!(validate_url(homepage, "homepage"));
112+
try!(validate_url(documentation, "documentation"));
113+
try!(validate_url(repository, "repository"));
114114

115115
match license {
116116
// If a license is given, validate it to make sure it's actually a
@@ -178,23 +178,24 @@ impl Crate {
178178
&[&ret.id, &user_id, &now]));
179179
return Ok(ret);
180180

181-
fn validate_url(url: Option<&str>) -> CargoResult<()> {
181+
fn validate_url(url: Option<&str>, field: &str) -> CargoResult<()> {
182182
let url = match url {
183183
Some(s) => s,
184184
None => return Ok(())
185185
};
186-
let url = match Url::parse(url) {
187-
Ok(url) => url,
188-
Err(..) => return Err(human(format!("not a valid url: {}", url)))
189-
};
186+
let url = try!(Url::parse(url).map_err(|_| {
187+
human(format!("`{}` is not a valid url: `{}`", field, url))
188+
}));
190189
match &url.scheme[..] {
191190
"http" | "https" => {}
192-
_ => return Err(human(format!("not a valid url scheme: {}", url)))
191+
s => return Err(human(format!("`{}` has an invalid url \
192+
scheme: `{}`", field, s)))
193193
}
194194
match url.scheme_data {
195195
url::SchemeData::Relative(..) => {}
196196
url::SchemeData::NonRelative(..) => {
197-
return Err(human(format!("not a valid url scheme: {}", url)))
197+
return Err(human(format!("`{}` must have relative scheme \
198+
data: {}", field, url)))
198199
}
199200
}
200201
Ok(())

0 commit comments

Comments
 (0)