fix-up & enhance advanced error handling

new (internal used) functions:
- _exit
- _exit_report
- _exit_sigint

these are used to fix several (wrong) error handlings.

new environment variable:
- UNATTENDED_PATCHING

by default we assume unattended patching, i.e. if an error occurs during
the patch, reset or any other process we will report the error and
auto close the shell. this is needed as we source functions and code and
so cannot simply terminate a master process. instead the whole shell will
be terminated so if an error occurs nothing else will be executed (and you
should notice easily that something is wrong).

without that a (serious) error can still continue the rest of a function
and you likely not even noticing the error itself.

you can use:

export UNATTENDED_PATCHING=0

before or after sourcing init.sh and it will *NOT* auto-close the shell.
that way you can check the output and fix any issues.

Signed-off-by: steadfasterX <steadfasterX@gmail.com>
This commit is contained in:
steadfasterX 2024-04-11 16:53:25 +00:00
parent 0ca1c41e8b
commit d812305b49
No known key found for this signature in database
GPG Key ID: 018C6BAB0D2E62B5
2 changed files with 67 additions and 8 deletions

View File

@ -21,11 +21,22 @@ _fetchError(){
local error_line_number="$2";
local last_func="$3";
local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g");
# ignore when pressing TAB or sim.
if [[ "$file" =~ .*bash_completion ]];then return; fi
case $last_func in
command_not_found_handle|_filedir) return;;
esac
if [ ! -z "$last_func" ] && [ ! -z "$file" ];then
echo -e "\e[0;31mERROR: $file -> ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
elif [ ! -z "$last_func" ];then
echo -e "\e[0;31mERROR: ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
else
echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
fi
export TR_ERR=$last_status
_exit_report
}
export -f _fetchError;
@ -72,7 +83,7 @@ enterAndClear() {
export -f enterAndClear;
gitReset() {
git add -A && git reset --hard;
(git add -A && git reset --hard) || true;
}
export -f gitReset;
@ -86,16 +97,22 @@ applyPatchReal() {
git format-patch -1 HEAD --zero-commit --no-signature --output="$currentWorkingPatch";
fi;
fi;
else
echo "Applying (git am): $currentWorkingPatch - FAILED"
git am --abort || true
echo "Applying (patch fallback): $currentWorkingPatch"
patch -r - --no-backup-if-mismatch --forward --ignore-whitespace --verbose -p1 < $currentWorkingPatch
fi;
else
git apply "$@";
echo "Applying (as diff): $currentWorkingPatch";
git apply "$@";
fi;
}
export -f applyPatchReal;
applyPatch() {
currentWorkingPatch=$1;
set -E
if [ -f "$currentWorkingPatch" ]; then
if git apply --check "$@" &> /dev/null; then
applyPatchReal "$@";
@ -108,11 +125,13 @@ applyPatch() {
echo "Applied (as 3way): $currentWorkingPatch";
else
echo -e "\e[0;31mERROR: Cannot apply: $currentWorkingPatch\e[0m";
false
fi;
fi;
fi;
else
echo -e "\e[0;31mERROR: Patch doesn't exist: $currentWorkingPatch\e[0m";
false
fi;
}
export -f applyPatch;

View File

@ -111,14 +111,15 @@ umask 0022;
export TR_ERR=0
export TR_PID=$$
unset nokill
if [ -z "$UNATTENDED_PATCHING" ];then export UNATTENDED_PATCHING=1;fi
set -E; #required for resetEnv()
resetEnv(){
trap - ERR EXIT USR2 SIGINT SIGHUP TERM
echo -e "\n\e[0;32mThe environment has been reset.\e[0m\nRemember to always '\e[0;31msource ../../Scripts/init.sh\e[0m' before building.\n"
set +E +f
}
export -f resetEnv
}; export -f resetEnv
# print result
# will also ensure the corresponding status code gets returned properly
@ -129,8 +130,47 @@ _errorReport(){
echo -e "\n\e[0;32m[FINAL RESULT] No error detected (please check the above output nevertheless!)\e[0m"
fi
return $TR_ERR
}
export -f _errorReport
}; export -f _errorReport
# exit
_exit(){
if [ "$1" == "noreset" ] || [ $TR_ERR -eq 0 ] ;then
echo -e "Ended with $TR_ERR.\nThe shell env has NOT been reset, type: resetEnv if needed.\n"
else
if [ -z "$nokill" ];then nokill=0;fi
resetEnv
echo -e "\nExecution has been STOPPED (TR_ERR=$TR_ERR)."
if [ "$UNATTENDED_PATCHING" -eq 1 ];then
echo -e "\n\e[0;31mPressing any key or waiting 10s will close this shell (set UNATTENDED_PATCHING=0 to disable auto-close)!\e[0m"
read -t 10 -p "- press any key to exit the shell NOW (auto closes after 10s) -" DUMMY || true
else
read -p "- press any key to exit the shell NOW -" DUMMY || true
fi
_SPIDS=$(ps -s $TR_PID -o pid= | tr '\n' ' ')
if [ -z "$_SPIDS" ];then
echo -e "... ok, no childs running (I am: $TR_PID)"
else
echo -e "... killing childs: $_SPIDS"
kill -9 $_SPIDS
fi
if [ $nokill -eq 0 ];then
echo "... killing shell: $TR_PID"
kill -9 $TR_PID
fi
fi
}; export -f _exit
# exit & reset & report
_exit_report(){
_errorReport
_exit
}; export -f _exit_report
# exit without reset/kill
_exit_sigint(){
echo -e "\n\nCTRL+C pressed or process has been terminated.."
_exit noreset
}; export _exit_sigint
# trap and print errors
# ERR: needed to fetch aborts when set -e is set
@ -138,8 +178,8 @@ trap 'E=$?; \
[ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \
&& export TR_ERR=$((TR_ERR + $E))' EXIT ERR
trap _errorReport USR2
trap resetEnv SIGINT SIGHUP TERM
trap _exit_report SIGUSR2 USR2
trap _exit_sigint SIGINT SIGHUP TERM
gpgVerifyGitHead() {
if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then