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

Commit 59d8f35

Browse files
committed
make divest functions error aware
this commit adds (the currently non-existing) error handling when using divest's functions and scripts. all the magic here gets activated when `source ../../Scripts/init.sh` gets executed which is already a mandatory step before starting any of the divest functions. when something fails during patching, resetting or building each error will be catched + printed including an error code now. last but not least the executed file and the line number causing that failure will be shown, too. as all divest functions get source'd and so not a single build script gets executed all ERR's needs to be trapped to catch issues. I am not aware of another way to handle that properly as sourcing means we cannot track a script or smth while this approach here just works. Example for an error thrown in a function call: > ERROR: $DOS_WORKSPACE_ROOT/Scripts/Common/Functions.sh -> verifyAllPlatformTags() ended with status >1< at line >49< Final SUCCESS result message after using `patchWorkspace`: > [FINAL RESULT] No error detected (please check the above output nevertheless!) Final ERROR result message after using `patchWorkspace`: > [FINAL RESULT] Serious error(s) found!!! > Summary error code was: 126. Check & fix all error lines above Some notes: - when an error occurs the process continues until the end (like it is now) i.e. an error will not stop the current and following tasks - when multiple errors occur the exit codes will be summed - buildDevice: a (summed) end result gets printed (SUCCESS or ERROR) at the very end - the trap used to catch any error will also be active for any command executed on the cli. that means: type "false" -> ENTER and you will get an error, too same for any script exectued after source init.sh - when all goes well the trap will be resetted at the end but there are cases where this might not happen -> that is why `resetEnv` can be executed to reset the trap, i.e. all becomes as it was before sourcing init.sh - `resetEnv` gets called automatically: - after a successful `patchWorkspace` run - whenever CTRL+C is used (during a running task or just on the cli) - a process get killed (SIGHUP, TERM) - the whole implementation might not catch all errors though - it highly depends on how the function or the script/program called actually handles errors or better said: if they return a proper exit code on failures. For example some tools (like some git cmds) might print an error but don't return a non-zero exit code. This cannot be tracked other then with your eyes or these must be replaced by other methods returning a non-zero exit code on failures. Signed-off-by: steadfasterX <[email protected]>
1 parent f4c2da9 commit 59d8f35

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

Scripts/Common/Functions.sh

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
#along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
umask 0022;
1818

19+
_fetchError(){
20+
local last_status="$1";
21+
local error_line_number="$2";
22+
local last_func="$3";
23+
local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g");
24+
if [ ! -z "$last_func" ] && [ ! -z "$file" ];then
25+
echo -e "\e[0;31mERROR: $file -> ${last_func}() ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
26+
else
27+
echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m";
28+
fi
29+
}
30+
export -f _fetchError;
31+
1932
startPatcher() {
2033
java -jar "$DOS_BINARY_PATCHER" patch workspace "$DOS_BUILD_BASE" "$DOS_WORKSPACE_ROOT""Patches/Linux/" "$DOS_SCRIPTS_CVES" $1;
2134
}

Scripts/Common/Post.sh

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ awk -i inplace '!/config_pinnerCameraApp/' device/*/*/overlay/frameworks/base/co
4343

4444
cd "$DOS_BUILD_BASE";
4545
echo -e "\e[0;32m[SCRIPT COMPLETE] Post tweaks complete\e[0m";
46+
47+
kill -USR2 $TR_PID

Scripts/init.sh

+34-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ export DOS_THEME_700="E64A19"; #Deep Orange 700
109109

110110
umask 0022;
111111

112+
export TR_ERR=0
113+
export TR_PID=$$
114+
115+
set -E; #required for resetEnv()
116+
resetEnv(){
117+
trap - ERR EXIT USR2 SIGINT SIGHUP TERM
118+
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"
119+
set +E +f
120+
}
121+
export -f resetEnv
122+
123+
# print result
124+
# will also ensure the corresponding status code gets returned properly
125+
_errorReport(){
126+
if [ "$TR_ERR" -ne 0 ];then
127+
echo -e "\n\e[0;31m[FINAL RESULT] Serious error(s) found!!!\nSummary error code was: $TR_ERR. Check & fix all error lines above\e[0m"
128+
else
129+
echo -e "\n\e[0;32m[FINAL RESULT] No error detected (please check the above output nevertheless!)\e[0m"
130+
fi
131+
return $TR_ERR
132+
}
133+
export -f _errorReport
134+
135+
# trap and print errors
136+
# ERR: needed to fetch aborts when set -e is set
137+
trap 'E=$?; \
138+
[ $E -ne 0 ] && _fetchError $E $LINENO $FUNCNAME $BASH_SOURCE \
139+
&& export TR_ERR=$((TR_ERR + $E))' EXIT ERR
140+
141+
trap _errorReport USR2
142+
trap resetEnv SIGINT SIGHUP TERM
143+
112144
gpgVerifyGitHead() {
113145
if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then
114146
if git -C "$1" verify-commit HEAD &>/dev/null; then
@@ -188,5 +220,5 @@ source "$DOS_SCRIPTS_COMMON/Functions.sh";
188220
source "$DOS_SCRIPTS_COMMON/Tag_Verifier.sh";
189221
source "$DOS_SCRIPTS/Functions.sh";
190222

191-
[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!";
192-
[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!";
223+
[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!" || true;
224+
[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!" || true;

0 commit comments

Comments
 (0)