From 492b79ec7a1844700ff75e19b39e3bc21f93dc23 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 31 Jan 2021 16:38:21 +0100 Subject: feat(users/Profpatsch): add die_* helpers for semantic exit errors There is this semantic exit code schema championed by execline and skaware tooling, and we refined and documented it a bit in lorri https://github.com/nix-community/lorri/blob/d1d673d42090f0cfe8ab9b92b465315a9e7d30a3/src/ops/mod.rs#L24-L35 in the past. This just transcribes the error messages into simple helper functions. Applies the functions to the places where we would panic or die `sys::exit()` instead. Change-Id: I15ca05cd6f99a25a3378518be94110eab416354e Reviewed-on: https://cl.tvl.fyi/c/depot/+/2475 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/exec_helpers.rs | 55 ++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'users/Profpatsch/execline') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index 4e4149882b40..3e74ffc72210 100644 --- a/users/Profpatsch/execline/exec_helpers.rs +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -36,6 +36,59 @@ pub fn exec_into_args<'a, 'b, Args, Arg, Env, Key, Val>(current_prog_name: &str, let env = env_additions.into_iter().collect::>(); let env = env.iter().map(|(k,v)| (OsStr::from_bytes(k.as_ref()), OsStr::from_bytes(v.as_ref()))); let err = std::process::Command::new(prog).args(args).envs(env).exec(); - panic!("{}: exec failed: {:?}", current_prog_name, err); + die_missing_executable(current_prog_name, format!("exec failed: {:?}", err)); } +/// Exit 1 to signify a generic expected error +/// (e.g. something that sometimes just goes wrong, like a nix build). +pub fn die_expected_error(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(1, current_prog_name, msg) +} + +/// Exit 100 to signify a user error (“the user is holding it wrong”). +/// This is a permanent error, if the program is executed the same way +/// it should crash with 100 again. +pub fn die_user_error(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(100, current_prog_name, msg) +} + +/// Exit 101 to signify an unexpected crash (failing assertion or panic). +/// This is the same exit code that `panic!()` emits. +pub fn die_panic(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(101, current_prog_name, msg) +} + +/// Exit 111 to signify a temporary error (such as resource exhaustion) +pub fn die_temporary(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(111, current_prog_name, msg) +} + +/// Exit 126 to signify an environment problem +/// (the user has set up stuff incorrectly so the program cannot work) +pub fn die_environment_problem(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(126, current_prog_name, msg) +} + +/// Exit 127 to signify a missing executable. +pub fn die_missing_executable(current_prog_name: &str, msg: S) -> ! +where S: AsRef +{ + die_with(127, current_prog_name, msg) +} + +fn die_with(status: i32, current_prog_name: &str, msg: S) -> ! + where S: AsRef +{ + eprintln!("{}: {}", current_prog_name, msg.as_ref()); + std::process::exit(status) +} -- cgit 1.4.1