Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: __BACKTRACE_EXITCODE__ which stores original exit code #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/util/exception.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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]}"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -253,6 +260,7 @@ Exception::DumpBacktrace() {
then
local -a trace=( $(caller $i) )

echo "0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the 0 here?

echo "${trace[0]}"
echo "${trace[1]}"
echo "${trace[@]:2}"
Expand Down
9 changes: 7 additions & 2 deletions lib/util/tryCatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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
Expand All @@ -29,14 +31,15 @@ Exception::ResetStore() {
Exception::GetLastException() {
if [[ -s $__oo__storedExceptionFile ]]
then
cat $__oo__storedExceptionErrorcodeFile
cat $__oo__storedExceptionLineFile
cat $__oo__storedExceptionFile
cat $__oo__storedExceptionSourceFile
cat $__oo__storedExceptionBacktraceFile

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
}

Expand All @@ -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]}"
Expand Down