Skip to content

Commit a107c9f

Browse files
authored
Merge pull request #1630 from NickeZ/fix-panic-2
Propagate io and terminal errors upward and ignore EPIPE
2 parents 31e9136 + add59bd commit a107c9f

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rustup-cli/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ error_chain! {
1818
foreign_links {
1919
Temp(temp::Error);
2020
Io(io::Error);
21+
Term(term::Error);
2122
}
2223

2324
errors {

src/rustup-cli/rustup_mode.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ use std::iter;
1515
use std::path::Path;
1616
use std::process::{self, Command};
1717

18+
fn handle_epipe(res: Result<()>) -> Result<()> {
19+
match res {
20+
Err(Error(ErrorKind::Io(ref err), _)) if err.kind() == std::io::ErrorKind::BrokenPipe => {
21+
Ok(())
22+
}
23+
res => res,
24+
}
25+
}
26+
1827
pub fn main() -> Result<()> {
1928
crate::self_update::cleanup_self_updater()?;
2029

@@ -30,8 +39,8 @@ pub fn main() -> Result<()> {
3039

3140
match matches.subcommand() {
3241
("show", Some(c)) => match c.subcommand() {
33-
("active-toolchain", Some(_)) => show_active_toolchain(cfg)?,
34-
(_, _) => show(cfg)?,
42+
("active-toolchain", Some(_)) => handle_epipe(show_active_toolchain(cfg))?,
43+
(_, _) => handle_epipe(show(cfg))?,
3544
},
3645
("install", Some(m)) => update(cfg, m)?,
3746
("update", Some(m)) => update(cfg, m)?,
@@ -667,11 +676,11 @@ fn show(cfg: &Cfg) -> Result<()> {
667676
// Print host triple
668677
{
669678
let mut t = term2::stdout();
670-
let _ = t.attr(term2::Attr::Bold);
671-
let _ = write!(t, "Default host: ");
672-
let _ = t.reset();
673-
println!("{}", cfg.get_default_host_triple()?);
674-
println!();
679+
t.attr(term2::Attr::Bold)?;
680+
write!(t, "Default host: ")?;
681+
t.reset()?;
682+
writeln!(t, "{}", cfg.get_default_host_triple()?)?;
683+
writeln!(t)?;
675684
}
676685

677686
let ref cwd = utils::current_dir()?;
@@ -712,80 +721,84 @@ fn show(cfg: &Cfg) -> Result<()> {
712721
> 1;
713722

714723
if show_installed_toolchains {
724+
let mut t = term2::stdout();
715725
if show_headers {
716-
print_header("installed toolchains")
726+
print_header(&mut t, "installed toolchains")?;
717727
}
718728
let default_name = cfg.get_default()?;
719-
for t in installed_toolchains {
720-
if default_name == t {
721-
println!("{} (default)", t);
729+
for it in installed_toolchains {
730+
if default_name == it {
731+
writeln!(t, "{} (default)", it)?;
722732
} else {
723-
println!("{}", t);
733+
writeln!(t, "{}", it)?;
724734
}
725735
}
726736
if show_headers {
727-
println!()
737+
writeln!(t)?
728738
};
729739
}
730740

731741
if show_active_targets {
742+
let mut t = term2::stdout();
732743
if show_headers {
733-
print_header("installed targets for active toolchain");
744+
print_header(&mut t, "installed targets for active toolchain")?;
734745
}
735-
for t in active_targets {
736-
println!(
746+
for at in active_targets {
747+
writeln!(
748+
t,
737749
"{}",
738-
t.component
750+
at.component
739751
.target
740752
.as_ref()
741753
.expect("rust-std should have a target")
742-
);
754+
)?;
743755
}
744756
if show_headers {
745-
println!()
757+
writeln!(t)?;
746758
};
747759
}
748760

749761
if show_active_toolchain {
762+
let mut t = term2::stdout();
750763
if show_headers {
751-
print_header("active toolchain")
764+
print_header(&mut t, "active toolchain")?;
752765
}
753766

754767
match active_toolchain {
755768
Ok(atc) => match atc {
756769
Some((ref toolchain, Some(ref reason))) => {
757-
println!("{} ({})", toolchain.name(), reason);
758-
println!("{}", common::rustc_version(toolchain));
770+
writeln!(t, "{} ({})", toolchain.name(), reason)?;
771+
writeln!(t, "{}", common::rustc_version(toolchain))?;
759772
}
760773
Some((ref toolchain, None)) => {
761-
println!("{} (default)", toolchain.name());
762-
println!("{}", common::rustc_version(toolchain));
774+
writeln!(t, "{} (default)", toolchain.name())?;
775+
writeln!(t, "{}", common::rustc_version(toolchain))?;
763776
}
764777
None => {
765-
println!("no active toolchain");
778+
writeln!(t, "no active toolchain")?;
766779
}
767780
},
768781
Err(err) => {
769782
if let Some(cause) = err.source() {
770-
println!("(error: {}, {})", err, cause);
783+
writeln!(t, "(error: {}, {})", err, cause)?;
771784
} else {
772-
println!("(error: {})", err);
785+
writeln!(t, "(error: {})", err)?;
773786
}
774787
}
775788
}
776789

777790
if show_headers {
778-
println!()
779-
};
791+
writeln!(t)?
792+
}
780793
}
781794

782-
fn print_header(s: &str) {
783-
let mut t = term2::stdout();
784-
let _ = t.attr(term2::Attr::Bold);
785-
let _ = writeln!(t, "{}", s);
786-
let _ = writeln!(t, "{}", iter::repeat("-").take(s.len()).collect::<String>());
787-
let _ = writeln!(t, "");
788-
let _ = t.reset();
795+
fn print_header(t: &mut term2::Terminal<std::io::Stdout>, s: &str) -> Result<()> {
796+
t.attr(term2::Attr::Bold)?;
797+
writeln!(t, "{}", s)?;
798+
writeln!(t, "{}", iter::repeat("-").take(s.len()).collect::<String>())?;
799+
writeln!(t)?;
800+
t.reset()?;
801+
Ok(())
789802
}
790803

791804
Ok(())

0 commit comments

Comments
 (0)