Browse Source

before stacktrace as array...

main
pvincent 1 week ago
parent
commit
7e0dcc5c4b
  1. 16
      bin/bash-back
  2. 2
      lib/utility.bash

16
bin/bash-back

@ -9,7 +9,7 @@ SCRIPT=$1
function on_return { function on_return {
if [[ "${BASH_COMMAND}" == return* ]]; then if [[ "${BASH_COMMAND}" == return* ]]; then
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) # regex does not work here!
code=$(echo "${BASH_COMMAND}" | cut -d' ' -f2) # regex does not work here unfortunately! thus use of basic cut
if [[ -n "$code" ]] && [[ $code -ne 0 ]]; then if [[ -n "$code" ]] && [[ $code -ne 0 ]]; then
trap - DEBUG trap - DEBUG
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}" >&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: return $code from ${FUNCNAME[1]}"
@ -20,11 +20,8 @@ function on_return {
# overridden function which permits stacktracing of original `return` statement # overridden function which permits stacktracing of original `return` statement
function exit { function exit {
code=${1:-0} code=${1:-0}
# TODO: prefer this output, but change the exit case scenario message in stacktrace first!
# >&2 echo "${BASH_SOURCE[2]}:${BASH_LINENO[0]} exit $@"
>&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: exit $@"
>&2 echo "${BASH_SOURCE[2]}:${BASH_LINENO[0]} exit $@"
# >&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: exit $@"
builtin exit $code builtin exit $code
} }
@ -60,8 +57,9 @@ function on_exit {
last_error=$(tail -n1 $TMP_ERROR) last_error=$(tail -n1 $TMP_ERROR)
last_source=${stack_sources[0]} last_source=${stack_sources[0]}
last_source=${last_source//./"\."} # replace . by \. last_source=${last_source//./"\."} # replace . by \.
regex="^$last_source: line ([0-9]+): (.*)\$"
if [[ "$last_error" =~ $regex ]]; then
regex1="^$last_source: line ([0-9]+): (.*)\$"
regex2="^$last_source:([0-9]+) (.*)\$"
if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then
line=${BASH_REMATCH[1]} line=${BASH_REMATCH[1]}
message=${BASH_REMATCH[2]} message=${BASH_REMATCH[2]}
regex=': unbound variable$' regex=': unbound variable$'
@ -78,7 +76,7 @@ function on_exit {
>&4 echo -e "ERROR $code: [$message]" >&4 echo -e "ERROR $code: [$message]"
else else
>&4 echo "ERROR $code: <return>"
>&4 echo "ERROR $code: <undefined>"
fi fi
for i in "${!stack_functions[@]}"; do for i in "${!stack_functions[@]}"; do

2
lib/utility.bash

@ -4,9 +4,9 @@ function blabla {
} }
function suite { function suite {
echo $nope
exit 10 # youpi exit 10 # youpi
exit exit
echo $nope
command_not_found command_not_found
return 7 return 7
return return

Loading…
Cancel
Save