This commit is contained in:
CanbiZ 2025-05-23 08:27:13 +02:00
parent dbcd1db4dc
commit 4369db931c
2 changed files with 30 additions and 2 deletions

View File

@ -29,7 +29,7 @@ fi
# This function enables error handling in the script by setting options and defining a trap for the ERR signal. # This function enables error handling in the script by setting options and defining a trap for the ERR signal.
catch_errors() { catch_errors() {
set -Eeuo pipefail set -Eeo pipefail
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
} }

View File

@ -9,6 +9,11 @@
# } # }
# fi # fi
trap 'on_error $? $LINENO' ERR
trap 'on_exit' EXIT
trap 'on_interrupt' INT
trap 'on_terminate' TERM
if ! declare -f wait_for >/dev/null; then if ! declare -f wait_for >/dev/null; then
wait_for() { wait_for() {
true true
@ -38,6 +43,30 @@ load_functions() {
# add more # add more
} }
on_error() {
local exit_code="$1"
local lineno="$2"
msg_error "Script failed at line $lineno with exit code $exit_code"
# Optionally log to your API or file here
exit "$exit_code"
}
on_exit() {
# Always called on script exit, success or failure
cleanup_temp_files || true
msg_info "Script exited"
}
on_interrupt() {
msg_error "Interrupted by user (CTRL+C)"
exit 130
}
on_terminate() {
msg_error "Terminated by signal (TERM)"
exit 143
}
setup_trap_abort_handling() { setup_trap_abort_handling() {
trap '__handle_signal_abort SIGINT' SIGINT trap '__handle_signal_abort SIGINT' SIGINT
trap '__handle_signal_abort SIGTERM' SIGTERM trap '__handle_signal_abort SIGTERM' SIGTERM
@ -165,7 +194,6 @@ default_vars() {
RETRY_EVERY=3 RETRY_EVERY=3
i=$RETRY_NUM i=$RETRY_NUM
#[[ "${VAR_OS:-}" == "unknown" ]] #[[ "${VAR_OS:-}" == "unknown" ]]
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------