From 94e8a8e3a3f4473f64205f5ed343df4708c686fa Mon Sep 17 00:00:00 2001 From: pvincent Date: Sun, 26 Jul 2026 12:20:39 +0400 Subject: [PATCH] poc miaou_debug --- poc | 51 +++++++++++++++++++++++++++++------------ test/stub/scenario.bash | 6 +++-- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/poc b/poc index 68d262e..b9f7144 100755 --- a/poc +++ b/poc @@ -2,11 +2,23 @@ # constants +MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)") +MIAOU_DEBUG=${MIAOU_DEBUG:-false} +readonly MIAOU_BASH_ERROR MIAOU_DEBUG + ## functions +function is_true { + [[ "${1:-}" == 'true' ]] +} + +function miaou_debug { + is_true "${MIAOU_DEBUG}" && >/dev/tty builtin echo -e "\e[90mDEBUG <==> \e[95m$*\e[0m" || true +} + function on_error { - code="${1:-0}" - >&2 echo on_error: $$ $BASHPID + code="${1:-100}" + miaou_debug on_error: $$ $BASHPID if [[ $$ == "$BASHPID" ]]; then >&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: command_error: ${code}" exit "$code" @@ -14,35 +26,44 @@ function on_error { >&2 echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: subshell_error: ${code}" kill -PIPE $$ fi - # kill -INT $BASHPID - # kill -INT $$ } function on_exit { code="${1:-0}" - echo "on_exit $code" + + miaou_debug --stderr-- + mapfile -t tmp_error <"$MIAOU_BASH_ERROR" + for i in "${tmp_error[@]}"; do >&4 echo -e "$i"; done + last_error="${tmp_error[-1]}" + miaou_debug --end of stderr-- + + miaou_debug "on_exit $code" } function on_pipe { - >&2 echo on_pipe: $$ $BASHPID - #TODO: compute code from latest sterr in case of command_error - code="${1:-24}" # 24 => CTRL-C + local code=141 # default code error for pipe + local tmp_error regex - trap - EXIT ERR PIPE + mapfile -t tmp_error <"$MIAOU_BASH_ERROR" + last_error="${tmp_error[-1]}" + regex='.*: line [0-9]+: subshell_error: ([0-9]+)' + [[ $last_error =~ $regex ]] && code=${BASH_REMATCH[1]} + + miaou_debug on_pipe: "${code}" exit "$code" } ## main set -euo pipefail -set -T # DEBUG RETURN -set -E # ERR on subshell -# set -b +set -E # HALT on subshell error +# set -T # DEBUG RETURN trap 'on_error $?' ERR trap 'on_exit $?' EXIT trap 'on_pipe' PIPE -echo "${BASHPID}" - -source ./test/stub/scenario.bash subshell_term +echo "--SOURCE scenario ${BASHPID}" +exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr +source ./test/stub/scenario.bash subshell_term 2>&3 +echo "--SUCCESS scenario" diff --git a/test/stub/scenario.bash b/test/stub/scenario.bash index 0da564a..01f132b 100755 --- a/test/stub/scenario.bash +++ b/test/stub/scenario.bash @@ -14,9 +14,11 @@ function do_subshell_false { function do_subshell_term { - # grep none none + echo toto + # echo 'toto' | grep none - echo "found1=$(grep nope nope)" + echo -n "found1=$(grep nope nope)" + echo "found1=$(grep nope nope | grep none)" echo "found2=$(command_not_found)" echo SHOULD NOT SHOW UP! }