Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Improve consistency of error messages
cloudhead committed 2 years ago
commit 2d7193f4bc7ab12133dbe8d07efb91626e3eba4a
parent ea4104dbd7315f6bd22a28454bb2e946b9f76759
5 files changed +18 -23
modified radicle-cli/src/commands/patch/list.rs
@@ -76,10 +76,7 @@ pub fn run(

    if !errors.is_empty() {
        for (title, id, error) in errors {
-
            term::error(format!(
-
                "{} Patch {title:?} ({id}) failed to load: {error}",
-
                term::format::negative("Error:")
-
            ));
+
            term::error(format!("patch {title:?} ({id}) failed to load: {error}",));
        }
    }

modified radicle-cli/src/commands/publish.rs
@@ -110,9 +110,9 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {

    if !validations.is_empty() {
        for err in validations {
-
            term::error(format!("Error: validation error {err}"));
+
            term::error(format!("validation error: {err}"));
        }
-
        anyhow::bail!("fatal error: repository storage is corrupt");
+
        anyhow::bail!("fatal: repository storage is corrupt");
    }

    term::success!(
modified radicle-cli/src/main.rs
@@ -25,7 +25,7 @@ fn main() {
        Ok(_) => process::exit(0),
        Err(err) => {
            if let Some(err) = err {
-
                term::error(format!("Error: rad: {err}"));
+
                term::error(format!("rad: {err}"));
            }
            process::exit(1);
        }
@@ -277,7 +277,7 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
            rad_remote::run,
            args.to_vec(),
        ),
-
        _ => {
+
        other => {
            let exe = format!("{NAME}-{exe}");
            let status = process::Command::new(exe.clone()).args(args).status();

@@ -289,7 +289,9 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
                }
                Err(err) => {
                    if let ErrorKind::NotFound = err.kind() {
-
                        return Err(Some(anyhow!("command `{}` not found", exe)));
+
                        return Err(Some(anyhow!(
+
                            "`{other}` is not a command. See `rad --help` for a list of commands.",
+
                        )));
                    } else {
                        return Err(Some(err.into()));
                    }
modified radicle-cli/src/terminal.rs
@@ -86,7 +86,7 @@ where
                }
                Some(Error::HelpManual) => {
                    let Ok(status) = term::manual(help.name) else {
-
                        perror(help.name, "failed to load manual page");
+
                        io::error(format!("rad {}: failed to load manual page", help.name));
                        process::exit(1);
                    };
                    process::exit(status.code().unwrap_or(0));
@@ -98,10 +98,10 @@ where
                Some(Error::WithHint { hint, .. }) => Some(hint),
                None => None,
            };
-
            perror(help.name, &err);
+
            io::error(format!("rad {}: {err}", help.name));

            if let Some(hint) = hint {
-
                eprintln!("{}", Paint::yellow(hint));
+
                io::hint(hint);
            }
            process::exit(1);
        }
@@ -128,23 +128,15 @@ pub fn profile() -> Result<Profile, anyhow::Error> {
    }
}

-
pub fn perror(name: &str, err: impl std::fmt::Display) {
-
    eprintln!(
-
        "{ERROR_PREFIX} {} rad {}: {err}",
-
        Paint::red("Error:"),
-
        name,
-
    );
-
}
-

pub fn fail(_name: &str, error: &anyhow::Error) {
    let err = error.to_string();
    let err = err.trim_end();

    for line in err.lines() {
-
        println!("{ERROR_PREFIX} {} {line}", Paint::red("Error:"));
+
        io::error(line);
    }

    if let Some(Error::WithHint { hint, .. }) = error.downcast_ref::<Error>() {
-
        println!("{ERROR_HINT_PREFIX} {}", Paint::yellow(hint));
+
        io::hint(hint);
    }
}
modified radicle-term/src/io.rs
@@ -174,7 +174,11 @@ pub fn warning(warning: impl fmt::Display) {
}

pub fn error(error: impl fmt::Display) {
-
    println!("{ERROR_PREFIX} {error}");
+
    println!("{ERROR_PREFIX} {} {error}", Paint::red("Error:"));
+
}
+

+
pub fn hint(hint: impl fmt::Display) {
+
    println!("{ERROR_HINT_PREFIX} {}", Paint::yellow(hint));
}

pub fn ask<D: fmt::Display>(prompt: D, default: bool) -> bool {