diff --git a/lib/util/exception.sh b/lib/util/exception.sh index 8cd562d..6179911 100644 --- a/lib/util/exception.sh +++ b/lib/util/exception.sh @@ -37,7 +37,7 @@ command_not_found_handle() { Exception::CustomCommandHandler "$@" && return 0 || true - local exit_code="${1}" + local exit_code="${1:-0}" shift || true # there might have not been any parameter, in which case "shift" would fail local script="${BASH_SOURCE[1]#./}" local lineNo="${BASH_LINENO[0]}" @@ -58,6 +58,9 @@ command_not_found_handle() { then subject=level3 Log "inside Try No.: $__oo__insideTryCatch" + if [[ ! -s $__oo__storedExceptionErrorcodeFile ]]; then + echo "$exit_code" > $__oo__storedExceptionErrorcodeFile + fi if [[ ! -s $__oo__storedExceptionLineFile ]]; then echo "$lineNo" > $__oo__storedExceptionLineFile fi @@ -74,13 +77,13 @@ command_not_found_handle() { return 1 # needs to be return 1 fi - if [[ $BASH_SUBSHELL -ge 25 ]] ## TODO: configurable + if (( $BASH_SUBSHELL >= 25 )) ## TODO: configurable then echo "ERROR: Call stack exceeded (25)." Exception::ContinueOrBreak || exit 1 fi - local -a exception=( "$lineNo" "$undefinedObject" "$script" ) + local -a exception=( "${exit_code:-0}" "$lineNo" "$undefinedObject" "$script" ) Exception::FillExceptionWithTraceElements @@ -107,6 +110,8 @@ Exception::PrintException() { #for traceElement in Exception::GetLastException while [[ $counter -lt ${#exception[@]} ]] do + backtraceErrorcode[$backtraceNo]="${exception[$counter]}" + counter+=1 backtraceLine[$backtraceNo]="${exception[$counter]}" counter+=1 backtraceCommand[$backtraceNo]="${exception[$counter]}" @@ -121,7 +126,7 @@ Exception::PrintException() { while [[ $index -lt $backtraceNo ]] do - Console::WriteStdErr "$(Exception::FormatExceptionSegment "${backtraceFile[$index]}" "${backtraceLine[$index]}" "${backtraceCommand[($index - 1)]}" $(( $index + $backtraceIndentationLevel )) )" + Console::WriteStdErr "$(Exception::FormatExceptionSegment "${backtraceErrorcode[$index]}" "${backtraceFile[$index]}" "${backtraceLine[$index]}" "${backtraceCommand[($index - 1)]}" $(( $index + $backtraceIndentationLevel )) )" index+=1 done } @@ -173,10 +178,12 @@ Exception::GetUnderlinedPart() { } Exception::FormatExceptionSegment() { - local script="$1" - local -i lineNo="$2" - local stringToMark="$3" - local -i callPosition="${4:-1}" + local errorcode="$1" + local script="$2" + local -i lineNo="$3" + local stringToMark="$4" + local -i callPosition="${5:-1}" + # [integer] errorcode # [string] script # [integer] lineNo # [string] stringToMark @@ -253,6 +260,7 @@ Exception::DumpBacktrace() { then local -a trace=( $(caller $i) ) + echo "0" echo "${trace[0]}" echo "${trace[1]}" echo "${trace[@]:2}" diff --git a/lib/util/tryCatch.sh b/lib/util/tryCatch.sh index 447dd9f..8445ac7 100644 --- a/lib/util/tryCatch.sh +++ b/lib/util/tryCatch.sh @@ -7,6 +7,7 @@ alias try='[[ $__oo__insideTryCatch -eq 0 ]] || __oo__presetShellOpts="$(echo $- alias catch='); declare __oo__tryResult=$?; __oo__insideTryCatch+=-1; [[ $__oo__insideTryCatch -lt 1 ]] || set -${__oo__presetShellOpts:-e} && Exception::Extract $__oo__tryResult || ' Exception::SetupTemp() { + declare -g __oo__storedExceptionErrorcodeFile="$(mktemp -t stored_exception_errorcode.$$.XXXXXXXXXX)" declare -g __oo__storedExceptionLineFile="$(mktemp -t stored_exception_line.$$.XXXXXXXXXX)" declare -g __oo__storedExceptionSourceFile="$(mktemp -t stored_exception_source.$$.XXXXXXXXXX)" declare -g __oo__storedExceptionBacktraceFile="$(mktemp -t stored_exception_backtrace.$$.XXXXXXXXXX)" @@ -15,11 +16,12 @@ Exception::SetupTemp() { Exception::CleanUp() { local exitVal=$? - rm -f $__oo__storedExceptionLineFile $__oo__storedExceptionSourceFile $__oo__storedExceptionBacktraceFile $__oo__storedExceptionFile || exit 1 + rm -f $__oo__storedExceptionErrorcodeFile $__oo__storedExceptionLineFile $__oo__storedExceptionSourceFile $__oo__storedExceptionBacktraceFile $__oo__storedExceptionFile || exit 1 exit $exitVal } Exception::ResetStore() { + > $__oo__storedExceptionErrorcodeFile > $__oo__storedExceptionLineFile > $__oo__storedExceptionFile > $__oo__storedExceptionSourceFile @@ -29,6 +31,7 @@ Exception::ResetStore() { Exception::GetLastException() { if [[ -s $__oo__storedExceptionFile ]] then + cat $__oo__storedExceptionErrorcodeFile cat $__oo__storedExceptionLineFile cat $__oo__storedExceptionFile cat $__oo__storedExceptionSourceFile @@ -36,7 +39,7 @@ Exception::GetLastException() { Exception::ResetStore else - echo -e "${BASH_LINENO[1]}\n \n${BASH_SOURCE[2]#./}" + echo -e "$?\n${BASH_LINENO[1]}\n \n${BASH_SOURCE[2]#./}" fi } @@ -54,6 +57,8 @@ Exception::Extract() { while [[ $counter -lt ${#__EXCEPTION__[@]} ]] do + __BACKTRACE_EXITCODE__[$backtraceNo]="${__EXCEPTION__[$counter]}" + counter+=1 __BACKTRACE_LINE__[$backtraceNo]="${__EXCEPTION__[$counter]}" counter+=1 __BACKTRACE_COMMAND__[$backtraceNo]="${__EXCEPTION__[$counter]}"