From 7724c3effd7d2b3459b0a3379977d9e84cab654e Mon Sep 17 00:00:00 2001 From: steadfasterX Date: Fri, 14 Apr 2023 08:24:46 +0000 Subject: [PATCH] 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 --- Scripts/Common/Functions.sh | 13 +++++++++++++ Scripts/Common/Post.sh | 2 ++ Scripts/init.sh | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Scripts/Common/Functions.sh b/Scripts/Common/Functions.sh index b9cd35ff..ed85e641 100644 --- a/Scripts/Common/Functions.sh +++ b/Scripts/Common/Functions.sh @@ -16,6 +16,19 @@ #along with this program. If not, see . umask 0022; +_fetchError(){ + local last_status="$1"; + local error_line_number="$2"; + local last_func="$3"; + local file=$(echo "$4" | sed "s#$DOS_WORKSPACE_ROOT#\$DOS_WORKSPACE_ROOT#g"); + 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"; + else + echo -e "\e[0;31mERROR: last command ended with status >${last_status}< at line >$((error_line_number -1))<\e[0m"; + fi +} +export -f _fetchError; + startPatcher() { java -jar "$DOS_BINARY_PATCHER" patch workspace "$DOS_BUILD_BASE" "$DOS_WORKSPACE_ROOT""Patches/Linux/" "$DOS_SCRIPTS_CVES" $1; } diff --git a/Scripts/Common/Post.sh b/Scripts/Common/Post.sh index a3c91472..ca257417 100644 --- a/Scripts/Common/Post.sh +++ b/Scripts/Common/Post.sh @@ -58,3 +58,5 @@ sed -i '/android.software.managed_users/s/notLowRam="true"//' frameworks/native/ cd "$DOS_BUILD_BASE"; echo -e "\e[0;32m[SCRIPT COMPLETE] Post tweaks complete\e[0m"; + +kill -USR2 $TR_PID diff --git a/Scripts/init.sh b/Scripts/init.sh index 1d06d163..e089293c 100644 --- a/Scripts/init.sh +++ b/Scripts/init.sh @@ -117,6 +117,38 @@ export DOS_THEME_700="E64A19"; #Deep Orange 700 umask 0022; +export TR_ERR=0 +export TR_PID=$$ + +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 + +# print result +# will also ensure the corresponding status code gets returned properly +_errorReport(){ + if [ "$TR_ERR" -ne 0 ];then + 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" + else + 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 + +# trap and print errors +# ERR: needed to fetch aborts when set -e is set +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 + gpgVerifyGitHead() { if [ -r "$DOS_TMP_GNUPG/pubring.kbx" ]; then if git -C "$1" verify-commit HEAD &>/dev/null; then @@ -203,5 +235,5 @@ source "$DOS_SCRIPTS_COMMON/Functions.sh"; source "$DOS_SCRIPTS_COMMON/Tag_Verifier.sh"; source "$DOS_SCRIPTS/Functions.sh"; -[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!"; -[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!"; +[[ -f "$DOS_BUILD_BASE/.repo/local_manifests/roomservice.xml" ]] && echo "roomservice manifest found! Please fix your manifests before continuing!" || true; +[[ -f "$DOS_BUILD_BASE/DOS_PATCHED_FLAG" ]] && echo "NOTE: THIS WORKSPACE IS ALREADY PATCHED, PLEASE RESET BEFORE PATCHING AGAIN!" || true;