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

Minor update to registry API error messages. #9213

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions crates/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ impl fmt::Display for ResponseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ResponseError::Curl(e) => write!(f, "{}", e),
ResponseError::Api { code, errors } => write!(
f,
"api errors (status {} {}): {}",
code,
reason(*code),
errors.join(", ")
),
ResponseError::Api { code, errors } => {
f.write_str("the remote server responded with an error")?;
if *code != 200 {
write!(f, " (status {} {})", code, reason(*code))?;
};
write!(f, ": {}", errors.join(", "))
}
ResponseError::Code {
code,
headers,
Expand Down
163 changes: 89 additions & 74 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,65 +286,62 @@ fn transmit(
None => BTreeMap::new(),
};

let publish = registry.publish(
&NewCrate {
name: pkg.name().to_string(),
vers: pkg.version().to_string(),
deps,
features: string_features,
authors: authors.clone(),
description: description.clone(),
homepage: homepage.clone(),
documentation: documentation.clone(),
keywords: keywords.clone(),
categories: categories.clone(),
readme: readme_content,
readme_file: readme.clone(),
repository: repository.clone(),
license: license.clone(),
license_file: license_file.clone(),
badges: badges.clone(),
links: links.clone(),
v: None,
},
tarball,
);

match publish {
Ok(warnings) => {
if !warnings.invalid_categories.is_empty() {
let msg = format!(
"the following are not valid category slugs and were \
ignored: {}. Please see https://crates.io/category_slugs \
for the list of all category slugs. \
",
warnings.invalid_categories.join(", ")
);
config.shell().warn(&msg)?;
}

if !warnings.invalid_badges.is_empty() {
let msg = format!(
"the following are not valid badges and were ignored: {}. \
Either the badge type specified is unknown or a required \
attribute is missing. Please see \
https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata \
for valid badge types and their required attributes.",
warnings.invalid_badges.join(", ")
);
config.shell().warn(&msg)?;
}
let warnings = registry
.publish(
&NewCrate {
name: pkg.name().to_string(),
vers: pkg.version().to_string(),
deps,
features: string_features,
authors: authors.clone(),
description: description.clone(),
homepage: homepage.clone(),
documentation: documentation.clone(),
keywords: keywords.clone(),
categories: categories.clone(),
readme: readme_content,
readme_file: readme.clone(),
repository: repository.clone(),
license: license.clone(),
license_file: license_file.clone(),
badges: badges.clone(),
links: links.clone(),
v: None,
},
tarball,
)
.chain_err(|| format!("failed to publish to registry at {}", registry.host()))?;

if !warnings.invalid_categories.is_empty() {
let msg = format!(
"the following are not valid category slugs and were \
ignored: {}. Please see https://crates.io/category_slugs \
for the list of all category slugs. \
",
warnings.invalid_categories.join(", ")
);
config.shell().warn(&msg)?;
}

if !warnings.other.is_empty() {
for msg in warnings.other {
config.shell().warn(&msg)?;
}
}
if !warnings.invalid_badges.is_empty() {
let msg = format!(
"the following are not valid badges and were ignored: {}. \
Either the badge type specified is unknown or a required \
attribute is missing. Please see \
https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata \
for valid badge types and their required attributes.",
warnings.invalid_badges.join(", ")
);
config.shell().warn(&msg)?;
}

Ok(())
if !warnings.other.is_empty() {
for msg in warnings.other {
config.shell().warn(&msg)?;
}
Err(e) => Err(e),
}

Ok(())
}

/// Returns the index and token from the config file for the given registry.
Expand Down Expand Up @@ -731,9 +728,9 @@ pub fn registry_login(
input
.lock()
.read_line(&mut line)
.chain_err(|| "failed to read stdin")
.map_err(anyhow::Error::from)?;
// Automatically remove `cargo login` from an inputted token to allow direct pastes from `registry.host()`/me.
.chain_err(|| "failed to read stdin")?;
// Automatically remove `cargo login` from an inputted token to
// allow direct pastes from `registry.host()`/me.
line.replace("cargo login", "").trim().to_string()
}
};
Expand Down Expand Up @@ -820,9 +817,13 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {

if let Some(ref v) = opts.to_add {
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
let msg = registry
.add_owners(&name, &v)
.map_err(|e| format_err!("failed to invite owners to crate {}: {}", name, e))?;
let msg = registry.add_owners(&name, &v).chain_err(|| {
format!(
"failed to invite owners to crate `{}` on registry at {}",
name,
registry.host()
)
})?;

config.shell().status("Owner", msg)?;
}
Expand All @@ -832,15 +833,23 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
config
.shell()
.status("Owner", format!("removing {:?} from crate {}", v, name))?;
registry
.remove_owners(&name, &v)
.chain_err(|| format!("failed to remove owners from crate {}", name))?;
registry.remove_owners(&name, &v).chain_err(|| {
format!(
"failed to remove owners from crate `{}` on registry at {}",
name,
registry.host()
)
})?;
}

if opts.list {
let owners = registry
.list_owners(&name)
.chain_err(|| format!("failed to list owners of crate {}", name))?;
let owners = registry.list_owners(&name).chain_err(|| {
format!(
"failed to list owners of crate `{}` on registry at {}",
name,
registry.host()
)
})?;
for owner in owners.iter() {
drop_print!(config, "{}", owner.login);
match (owner.name.as_ref(), owner.email.as_ref()) {
Expand Down Expand Up @@ -882,16 +891,19 @@ pub fn yank(
config
.shell()
.status("Unyank", format!("{}:{}", name, version))?;
registry
.unyank(&name, &version)
.chain_err(|| "failed to undo a yank")?;
registry.unyank(&name, &version).chain_err(|| {
format!(
"failed to undo a yank from the registry at {}",
registry.host()
)
})?;
} else {
config
.shell()
.status("Yank", format!("{}:{}", name, version))?;
registry
.yank(&name, &version)
.chain_err(|| "failed to yank")?;
.chain_err(|| format!("failed to yank from the registry at {}", registry.host()))?;
}

Ok(())
Expand Down Expand Up @@ -937,9 +949,12 @@ pub fn search(
}

let (mut registry, _, source_id) = registry(config, None, index, reg, false, false)?;
let (crates, total_crates) = registry
.search(query, limit)
.chain_err(|| "failed to retrieve search results from the registry")?;
let (crates, total_crates) = registry.search(query, limit).chain_err(|| {
format!(
"failed to retrieve search results from the registry at {}",
registry.host()
)
})?;

let names = crates
.iter()
Expand Down
7 changes: 5 additions & 2 deletions tests/testsuite/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ fn simple_add() {
.with_status(101)
.with_stderr(
" Updating `[..]` index
error: failed to invite owners to crate foo: EOF while parsing a value at line 1 column 0",
error: failed to invite owners to crate `foo` on registry at file://[..]

Caused by:
EOF while parsing a value at line 1 column 0",
)
.run();
}
Expand Down Expand Up @@ -111,7 +114,7 @@ fn simple_remove() {
.with_stderr(
" Updating `[..]` index
Owner removing [\"username\"] from crate foo
error: failed to remove owners from crate foo
error: failed to remove owners from crate `foo` on registry at file://[..]

Caused by:
EOF while parsing a value at line 1 column 0",
Expand Down
78 changes: 68 additions & 10 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,56 @@ fn api_error_json() {
[UPDATING] [..]
[PACKAGING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 [..]
[ERROR] api errors (status 403 Forbidden): you must be logged in
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/

Caused by:
the remote server responded with an error (status 403 Forbidden): you must be logged in
",
)
.run();

t.join().unwrap();
}

#[cargo_test]
fn api_error_200() {
// Registry returns an API error with a 200 status code.
let t = registry::RegistryBuilder::new().build_api_server(&|_headers| {
(
200,
&r#"{"errors": [{"detail": "max upload size is 123"}]}"#,
)
});

let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
documentation = "foo"
homepage = "foo"
repository = "foo"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("publish --no-verify --registry alternative")
.with_status(101)
.with_stderr(
"\
[UPDATING] [..]
[PACKAGING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 [..]
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/

Caused by:
the remote server responded with an error: max upload size is 123
",
)
.run();
Expand Down Expand Up @@ -1528,13 +1577,16 @@ fn api_error_code() {
[UPDATING] [..]
[PACKAGING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 [..]
[ERROR] failed to get a 200 OK response, got 400
headers:
<tab>HTTP/1.1 400
<tab>Content-Length: 7
<tab>
body:
go away
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/

Caused by:
failed to get a 200 OK response, got 400
headers:
<tab>HTTP/1.1 400
<tab>Content-Length: 7
<tab>
body:
go away
",
)
.run();
Expand Down Expand Up @@ -1577,7 +1629,10 @@ fn api_curl_error() {
[UPDATING] [..]
[PACKAGING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 [..]
[ERROR] [52] [..]
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/

Caused by:
[52] [..]
",
)
.run();
Expand Down Expand Up @@ -1616,7 +1671,10 @@ fn api_other_error() {
[UPDATING] [..]
[PACKAGING] foo v0.0.1 [..]
[UPLOADING] foo v0.0.1 [..]
[ERROR] invalid response from server
[ERROR] failed to publish to registry at http://127.0.0.1:[..]/

Caused by:
invalid response from server

Caused by:
response body was not valid utf-8
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn simple() {
.with_stderr(
" Updating `[..]` index
Unyank foo:0.0.1
error: failed to undo a yank
error: failed to undo a yank from the registry at file:///[..]

Caused by:
EOF while parsing a value at line 1 column 0",
Expand Down