From: Ian Kelling Date: Sat, 8 Sep 2018 01:16:09 +0000 (-0400) Subject: add err-print, slight output improvement, docs X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9ce3dca13276b2f0204a31c02384a1bc219c449d;p=errhandle.git add err-print, slight output improvement, docs --- diff --git a/err b/err index 4a68f0a..518b17e 100644 --- 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.