-
-
Notifications
You must be signed in to change notification settings - Fork 2k
who: report a stdout write error instead of panicking #13389
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,9 @@ | |
| use crate::options; | ||
| use crate::uu_app; | ||
|
|
||
| use thiserror::Error; | ||
| use uucore::display::Quotable; | ||
| use uucore::error::{FromIo, UResult}; | ||
| use uucore::error::{FromIo, UError, UResult, strip_errno}; | ||
| use uucore::libc::S_IWGRP; | ||
| use uucore::translate; | ||
|
|
||
|
|
@@ -25,6 +26,14 @@ fn get_long_usage() -> String { | |
| translate!("who-long-usage", "default_file" => utmpx::DEFAULT_FILE) | ||
| } | ||
|
|
||
| #[derive(Debug, Error)] | ||
| enum WhoError { | ||
| #[error("{}", translate!("who-error-write", "error" => strip_errno(.0)))] | ||
| Write(std::io::Error), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it could derive from? |
||
| } | ||
|
|
||
| impl UError for WhoError {} | ||
|
|
||
| pub fn uumain(args: impl uucore::Args) -> UResult<()> { | ||
| let matches = | ||
| uucore::clap_localization::handle_clap_result(uu_app().after_help(get_long_usage()), args)?; | ||
|
|
@@ -209,8 +218,16 @@ impl Who { | |
| .filter(UtmpxRecord::is_user_process) | ||
| .map(|ut| ut.user()) | ||
| .collect::<Vec<_>>(); | ||
| println!("{}", users.join(" ")); | ||
| println!("{}", translate!("who-user-count", "count" => users.len())); | ||
| let mut out = stdout().lock(); | ||
| writeln!(out, "{}", users.join(" ")) | ||
| .and_then(|()| { | ||
| writeln!( | ||
| out, | ||
| "{}", | ||
| translate!("who-user-count", "count" => users.len()) | ||
| ) | ||
| }) | ||
| .map_err(WhoError::Write)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then the map_err can be skipped |
||
| } else { | ||
| let records = utmpx::Utmpx::iter_all_records_from(f); | ||
|
|
||
|
|
@@ -450,7 +467,7 @@ impl Who { | |
| if self.include_exit { | ||
| write!(buf, " {exit:<12}").unwrap(); | ||
| } | ||
| writeln!(stdout(), "{}", buf.trim_end())?; | ||
| writeln!(stdout(), "{}", buf.trim_end()).map_err(WhoError::Write)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are more variants expected? Otherwise a
structseems more appropriate imo..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally define a
UtilErrorenum for each utility. In the case ofwho, I do expect additional variants in the future, for example, awho-canonicalize-errorvariant.