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

Commit 30859d2

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 fcec1c8 commit 30859d2

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

@@ -74,7 +85,7 @@ enterAndClear() {
7485
export -f enterAndClear;
7586

7687
gitReset() {
77-
git add -A && git reset --hard;
88+
(git add -A && git reset --hard) || true;
7889
}
7990
export -f gitReset;
8091

@@ -88,16 +99,22 @@ applyPatchReal() {
8899
git format-patch -1 HEAD --zero-commit --no-signature --output="$currentWorkingPatch";
89100
fi;
90101
fi;
102+
else
103+
echo "Applying (git am): $currentWorkingPatch - FAILED"
104+
git am --abort || true
105+
echo "Applying (patch fallback): $currentWorkingPatch"
106+
patch -r - --no-backup-if-mismatch --forward --ignore-whitespace --verbose -p1 < $currentWorkingPatch
91107
fi;
92108
else
93-
git apply "$@";
94109
echo "Applying (as diff): $currentWorkingPatch";
110+
git apply "$@";
95111
fi;
96112
}
97113
export -f applyPatchReal;
98114

99115
applyPatch() {
100116
currentWorkingPatch=$1;
117+
set -E
101118
if [ -f "$currentWorkingPatch" ]; then
102119
if git apply --check "$@" &> /dev/null; then
103120
applyPatchReal "$@";
@@ -110,11 +127,13 @@ applyPatch() {
110127
echo "Applied (as 3way): $currentWorkingPatch";
111128
else
112129
echo -e "\e[0;31mERROR: Cannot apply: $currentWorkingPatch\e[0m";
130+
false
113131
fi;
114132
fi;
115133
fi;
116134
else
117135
echo -e "\e[0;31mERROR: Patch doesn't exist: $currentWorkingPatch\e[0m";
136+
false
118137
fi;
119138
}
120139
export -f applyPatch;

Scripts/init.sh

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

120120
export TR_ERR=0
121121
export TR_PID=$$
122+
unset nokill
123+
if [ -z "$UNATTENDED_PATCHING" ];then export UNATTENDED_PATCHING=1;fi
122124

123125
set -E; #required for resetEnv()
124126
resetEnv(){
125127
trap - ERR EXIT USR2 SIGINT SIGHUP TERM
126128
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"
127129
set +E +f
128-
}
129-
export -f resetEnv
130+
}; export -f resetEnv
130131

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

143183
# trap and print errors
144184
# ERR: needed to fetch aborts when set -e is set
145185
trap 'E=$?; \
146186
[ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \
147187
&& export TR_ERR=$((TR_ERR + $E))' EXIT ERR
148188

149-
trap _errorReport USR2
150-
trap resetEnv SIGINT SIGHUP TERM
189+
trap _exit_report SIGUSR2 USR2
190+
trap _exit_sigint SIGINT SIGHUP TERM
151191

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

0 commit comments

Comments
 (0)