better error handling

This commit is contained in:
CanbiZ 2025-07-28 09:19:41 +02:00
parent 54aa5f7b2d
commit 313a5be068

View File

@ -31,10 +31,15 @@ function on_exit() {
exit "$exit_code" exit "$exit_code"
} }
function error_handler() { error_handler() {
local exit_code="$?" local exit_code="$?"
local line_number="$1" local line_number="$1"
local command="$2" local command="$2"
if [[ "${USER_EXITED:-false}" == "true" ]]; then
exit "$exit_code"
fi
printf "\e[?25h" printf "\e[?25h"
echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n" echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n"
exit "$exit_code" exit "$exit_code"
@ -45,12 +50,14 @@ function on_interrupt() {
exit 130 exit 130
} }
function on_terminate() { on_terminate() {
[[ "${USER_EXITED:-false}" == "true" ]] && exit 0
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}" echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
exit 143 exit 143
} }
exit_script() { exit_script() {
USER_EXITED=true
clear clear
printf "\e[?25h" printf "\e[?25h"
echo -e "\n${CROSS}${RD}User exited script${CL}\n" echo -e "\n${CROSS}${RD}User exited script${CL}\n"