add err-print, slight output improvement, docs
authorIan Kelling <iank@fsf.org>
Sat, 8 Sep 2018 01:16:09 +0000 (21:16 -0400)
committerIan Kelling <iank@fsf.org>
Sat, 8 Sep 2018 01:16:09 +0000 (21:16 -0400)
err

diff --git a/err b/err
index 4a68f0a00981b157649e1ebf93b1af3cbac7d7d8..518b17effbe1fcdcea9ede1a503929fa231978f7 100644 (file)
--- a/err
+++ b/err
@@ -29,7 +29,10 @@ err-bash-trace() {
   #
   # Note: It does not show function args unless you first run:
   # shopt -s extdebug
-  # err-catch runs this for you.
+  # which err-catch & err-print does for you.
+  #
+  # $1 is the frame to start printing on. default -1. Useful when
+  # printing from an ERR trap function.
 
   local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
   local source
@@ -67,9 +70,9 @@ err-catch() {
     err=$?
     exec >&2
     set +x
-    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
-    # err trap does not work within an error trap, the following line:
-    err-bash-trace 2; set -e
+    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err"
+    err-bash-trace 2
+    set -e # err trap does not work within an error trap
     "${_errcatch_cleanup[@]:-:}" # note :-: is to be compatible with set -u
     echo "$0: exiting with code $err"
     exit $err
@@ -78,6 +81,24 @@ err-catch() {
   set -o pipefail
 }
 
+err-print() {
+  # help: on errors: print stack trace
+  #
+  # This function depends on err-bash-trace.
+
+  set -E; shopt -s extdebug
+  _err-trap() {
+    err=$?
+    exec >&2
+    set +x
+    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err"
+    err-bash-trace 2
+  }
+  trap _err-trap ERR
+  set -o pipefail
+}
+
+
 err-exit() {
   # usage: err-exit [EXIT_CODE] [MESSAGE]
   # help: exit and print stack trace.