Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit d812305

Browse files
committed
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 <[email protected]>
1 parent 0ca1c41 commit d812305

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

Scripts/Common/Functions.sh

+21-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,22 @@ _fetchError(){
2121
local error_line_number="$2";
2222
local last_func="$3";
2323
local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g");
24+
25+
# ignore when pressing TAB or sim.
26+
if [[ "$file" =~ .*bash_completion ]];then return; fi
27+
case $last_func in
28+
command_not_found_handle|_filedir) return;;
29+
esac
30+
2431
if [ ! -z "$last_func" ] && [ ! -z "$file" ];then
2532
echo -e "\e[0;31mERROR: $file -> ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
33+
elif [ ! -z "$last_func" ];then
34+
echo -e "\e[0;31mERROR: ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
2635
else
2736
echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
2837
fi
38+
export TR_ERR=$last_status
39+
_exit_report
2940
}
3041
export -f _fetchError;
3142

@@ -72,7 +83,7 @@ enterAndClear() {
7283
export -f enterAndClear;
7384

7485
gitReset() {
75-
git add -A && git reset --hard;
86+
(git add -A && git reset --hard) || true;
7687
}
7788
export -f gitReset;
7889

@@ -86,16 +97,22 @@ applyPatchReal() {
8697
git format-patch -1 HEAD --zero-commit --no-signature --output="$currentWorkingPatch";
8798
fi;
8899
fi;
100+
else
101+
echo "Applying (git am): $currentWorkingPatch - FAILED"
102+
git am --abort || true
103+
echo "Applying (patch fallback): $currentWorkingPatch"
104+
patch -r - --no-backup-if-mismatch --forward --ignore-whitespace --verbose -p1 < $currentWorkingPatch
89105
fi;
90106
else
91-
git apply "$@";
92107
echo "Applying (as diff): $currentWorkingPatch";
108+
git apply "$@";
93109
fi;
94110
}
95111
export -f applyPatchReal;
96112

97113
applyPatch() {
98114
currentWorkingPatch=$1;
115+
set -E
99116
if [ -f "$currentWorkingPatch" ]; then
100117
if git apply --check "$@" &> /dev/null; then
101118
applyPatchReal "$@";
@@ -108,11 +125,13 @@ applyPatch() {
108125
echo "Applied (as 3way): $currentWorkingPatch";
109126
else
110127
echo -e "\e[0;31mERROR: Cannot apply: $currentWorkingPatch\e[0m";
128+
false
111129
fi;
112130
fi;
113131
fi;
114132
else
115133
echo -e "\e[0;31mERROR: Patch doesn't exist: $currentWorkingPatch\e[0m";
134+
false
116135
fi;
117136
}
118137
export -f applyPatch;

Scripts/init.sh

+46-6
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ umask 0022;
111111

112112
export TR_ERR=0
113113
export TR_PID=$$
114+
unset nokill
115+
if [ -z "$UNATTENDED_PATCHING" ];then export UNATTENDED_PATCHING=1;fi
114116

115117
set -E; #required for resetEnv()
116118
resetEnv(){
117119
trap - ERR EXIT USR2 SIGINT SIGHUP TERM
118120
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"
119121
set +E +f
120-
}
121-
export -f resetEnv
122+
}; export -f resetEnv
122123

123124
# print result
124125
# will also ensure the corresponding status code gets returned properly
@@ -129,17 +130,56 @@ _errorReport(){
129130
echo -e "\n\e[0;32m[FINAL RESULT] No error detected (please check the above output nevertheless!)\e[0m"
130131
fi
131132
return $TR_ERR
132-
}
133-
export -f _errorReport
133+
}; export -f _errorReport
134+
135+
# exit
136+
_exit(){
137+
if [ "$1" == "noreset" ] || [ $TR_ERR -eq 0 ] ;then
138+
echo -e "Ended with $TR_ERR.\nThe shell env has NOT been reset, type: resetEnv if needed.\n"
139+
else
140+
if [ -z "$nokill" ];then nokill=0;fi
141+
resetEnv
142+
echo -e "\nExecution has been STOPPED (TR_ERR=$TR_ERR)."
143+
if [ "$UNATTENDED_PATCHING" -eq 1 ];then
144+
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"
145+
read -t 10 -p "- press any key to exit the shell NOW (auto closes after 10s) -" DUMMY || true
146+
else
147+
read -p "- press any key to exit the shell NOW -" DUMMY || true
148+
fi
149+
_SPIDS=$(ps -s $TR_PID -o pid= | tr '\n' ' ')
150+
if [ -z "$_SPIDS" ];then
151+
echo -e "... ok, no childs running (I am: $TR_PID)"
152+
else
153+
echo -e "... killing childs: $_SPIDS"
154+
kill -9 $_SPIDS
155+
fi
156+
if [ $nokill -eq 0 ];then
157+
echo "... killing shell: $TR_PID"
158+
kill -9 $TR_PID
159+
fi
160+
fi
161+
}; export -f _exit
162+
163+
# exit & reset & report
164+
_exit_report(){
165+
_errorReport
166+
_exit
167+
}; export -f _exit_report
168+
169+
# exit without reset/kill
170+
_exit_sigint(){
171+
echo -e "\n\nCTRL+C pressed or process has been terminated.."
172+
_exit noreset
173+
}; export _exit_sigint
134174

135175
# trap and print errors
136176
# ERR: needed to fetch aborts when set -e is set
137177
trap 'E=$?; \
138178
[ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \
139179
&& export TR_ERR=$((TR_ERR + $E))' EXIT ERR
140180

141-
trap _errorReport USR2
142-
trap resetEnv SIGINT SIGHUP TERM
181+
trap _exit_report SIGUSR2 USR2
182+
trap _exit_sigint SIGINT SIGHUP TERM
143183

144184
gpgVerifyGitHead() {
145185
if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then

0 commit comments

Comments
 (0)