Skip to content

Commit e3b4b52

Browse files
committed
Use errors everywhere
1 parent 3739fed commit e3b4b52

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

crates/uv/src/commands/tool/upgrade.rs

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ pub(crate) async fn upgrade(
5555

5656
// Determine whether we applied any upgrades.
5757
let mut did_upgrade = false;
58-
// Determine whether a tool upgrade failed
58+
59+
// Determine whether any tool upgrade failed.
5960
let mut failed_upgrade = false;
6061

61-
for name in names {
62+
for name in &names {
6263
debug!("Upgrading tool: `{name}`");
6364
let changelog = upgrade_tool(
64-
&name,
65+
name,
6566
printer,
6667
&installed_tools,
6768
&args,
@@ -75,32 +76,32 @@ pub(crate) async fn upgrade(
7576

7677
match changelog {
7778
Ok(changelog) => {
78-
if let Some(changelog) = changelog {
79-
did_upgrade |= !changelog.is_empty();
80-
} else {
81-
failed_upgrade = true;
82-
}
79+
did_upgrade |= !changelog.is_empty();
8380
}
84-
Err(e) => {
81+
Err(err) => {
82+
// If we have a single tool, return the error directly.
83+
if names.len() == 1 {
84+
return Err(err);
85+
}
86+
8587
writeln!(
8688
printer.stderr(),
87-
"Failed to upgrade `{}` due to `{e}`",
89+
"Failed to upgrade `{}`: {err}",
8890
name.cyan(),
8991
)?;
90-
9192
failed_upgrade = true;
9293
}
9394
}
9495
}
9596

96-
if !did_upgrade && !failed_upgrade {
97-
writeln!(printer.stderr(), "Nothing to upgrade")?;
98-
}
99-
10097
if failed_upgrade {
10198
return Ok(ExitStatus::Failure);
10299
}
103100

101+
if !did_upgrade {
102+
writeln!(printer.stderr(), "Nothing to upgrade")?;
103+
}
104+
104105
Ok(ExitStatus::Success)
105106
}
106107

@@ -114,53 +115,45 @@ async fn upgrade_tool(
114115
connectivity: Connectivity,
115116
concurrency: Concurrency,
116117
native_tls: bool,
117-
) -> anyhow::Result<Option<Changelog>> {
118+
) -> Result<Changelog> {
118119
// Ensure the tool is installed.
119120
let existing_tool_receipt = match installed_tools.get_tool_receipt(name) {
120121
Ok(Some(receipt)) => receipt,
121122
Ok(None) => {
122123
let install_command = format!("uv tool install {name}");
123-
writeln!(
124-
printer.stderr(),
124+
return Err(anyhow::anyhow!(
125125
"`{}` is not installed; run `{}` to install",
126126
name.cyan(),
127127
install_command.green()
128-
)?;
129-
return Ok(None);
128+
));
130129
}
131130
Err(_) => {
132131
let install_command = format!("uv tool install --force {name}");
133-
writeln!(
134-
printer.stderr(),
132+
return Err(anyhow::anyhow!(
135133
"`{}` is missing a valid receipt; run `{}` to reinstall",
136134
name.cyan(),
137135
install_command.green()
138-
)?;
139-
return Ok(None);
136+
));
140137
}
141138
};
142139

143140
let existing_environment = match installed_tools.get_environment(name, cache) {
144141
Ok(Some(environment)) => environment,
145142
Ok(None) => {
146143
let install_command = format!("uv tool install {name}");
147-
writeln!(
148-
printer.stderr(),
144+
return Err(anyhow::anyhow!(
149145
"`{}` is not installed; run `{}` to install",
150146
name.cyan(),
151147
install_command.green()
152-
)?;
153-
return Ok(None);
148+
));
154149
}
155150
Err(_) => {
156151
let install_command = format!("uv tool install --force {name}");
157-
writeln!(
158-
printer.stderr(),
152+
return Err(anyhow::anyhow!(
159153
"`{}` is missing a valid environment; run `{}` to reinstall",
160154
name.cyan(),
161155
install_command.green()
162-
)?;
163-
return Ok(None);
156+
));
164157
}
165158
};
166159

@@ -216,5 +209,5 @@ async fn upgrade_tool(
216209
)?;
217210
}
218211

219-
Ok(Some(changelog))
212+
Ok(changelog)
220213
}

crates/uv/tests/tool_upgrade.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ fn test_tool_upgrade_non_existing_package() {
220220
.env("XDG_BIN_HOME", bin_dir.as_os_str())
221221
.env("PATH", bin_dir.as_os_str()), @r###"
222222
success: false
223-
exit_code: 1
223+
exit_code: 2
224224
----- stdout -----
225225
226226
----- stderr -----
227-
`black` is not installed; run `uv tool install black` to install
227+
error: `black` is not installed; run `uv tool install black` to install
228228
"###);
229229

230230
// Attempt to upgrade all.
@@ -315,7 +315,7 @@ fn test_tool_upgrade_not_stop_if_upgrade_fails() -> anyhow::Result<()> {
315315
+ babel==2.14.0
316316
- pytz==2018.5
317317
Installed 1 executable: pybabel
318-
`python-dotenv` is missing a valid receipt; run `uv tool install --force python-dotenv` to reinstall
318+
Failed to upgrade `python-dotenv`: `python-dotenv` is missing a valid receipt; run `uv tool install --force python-dotenv` to reinstall
319319
"###);
320320

321321
Ok(())

0 commit comments

Comments
 (0)