about summary refs log tree commit diff
path: root/users/Profpatsch/netencode
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-01-31T15·38+0100
committerProfpatsch <mail@profpatsch.de>2021-01-31T16·06+0000
commit492b79ec7a1844700ff75e19b39e3bc21f93dc23 (patch)
tree8b03f572a054bee26f511ee80c746c4ca15eb64c /users/Profpatsch/netencode
parent83634341aa6683e1b96717757557c7d83a89b3fd (diff)
feat(users/Profpatsch): add die_* helpers for semantic exit errors r/2176
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 <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/netencode')
-rw-r--r--users/Profpatsch/netencode/default.nix9
-rw-r--r--users/Profpatsch/netencode/netencode.rs7
2 files changed, 10 insertions, 6 deletions
diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix
index fb1d2c2ef8..b9822d4822 100644
--- a/users/Profpatsch/netencode/default.nix
+++ b/users/Profpatsch/netencode/default.nix
@@ -30,7 +30,10 @@ let
 
   netencode-rs-common = tests: imports.writers.rustSimpleLib {
     name = "netencode";
-    dependencies = [ nom ];
+    dependencies = [
+      nom
+      depot.users.Profpatsch.execline.exec-helpers
+    ];
     buildTests = tests;
     release = false;
     verbose = true;
@@ -101,13 +104,13 @@ let
     use netencode::dec::{Record, ScalarAsBytes, Decoder, DecodeError};
 
     fn main() {
-        let t = netencode::t_from_stdin_or_panic("record-splice-env");
+        let t = netencode::t_from_stdin_or_die_user_error("record-splice-env");
         let (_, prog) = exec_helpers::args_for_exec("record-splice-env", 0);
         match Record::<ScalarAsBytes>::dec(t) {
             Ok(map) => {
                 exec_helpers::exec_into_args("record-splice-env", prog, map);
             },
-            Err(DecodeError(err)) => panic!("{}", err),
+            Err(DecodeError(err)) => exec_helpers::die_user_error("record-splice-env", err),
         }
     }
   '';
diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs
index 66f3245fcf..f6158ae3e6 100644
--- a/users/Profpatsch/netencode/netencode.rs
+++ b/users/Profpatsch/netencode/netencode.rs
@@ -1,4 +1,5 @@
 extern crate nom;
+extern crate exec_helpers;
 
 use std::collections::HashMap;
 use std::io::{Write, Read};
@@ -116,15 +117,15 @@ pub fn text(s: String) -> T {
     T::Text(s)
 }
 
-pub fn t_from_stdin_or_panic(prog_name: &str) -> T {
+pub fn t_from_stdin_or_die_user_error(prog_name: &str) -> T {
     let mut buf = vec![];
     std::io::stdin().lock().read_to_end(&mut buf);
     match parse::t_t(&buf) {
         Ok((rest, t)) => match rest {
             b"" => t,
-            _ => panic!("{}: stdin contained some soup after netencode value: {:?}", prog_name, rest)
+            _ => exec_helpers::die_user_error(prog_name, format!("stdin contained some soup after netencode value: {:?}", rest))
         },
-        Err(err) => panic!("{}: unable to parse netencode from stdin: {:?}", prog_name, err)
+        Err(err) => exec_helpers::die_user_error(prog_name, format!("unable to parse netencode from stdin: {:?}", err))
     }
 }