From 4369db931cdcf89b02dbe1122d50eb40a4ae9e3d Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Fri, 23 May 2025 08:27:13 +0200 Subject: [PATCH] test --- misc/build.func | 2 +- misc/core.func | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/misc/build.func b/misc/build.func index 2c02c70..7adab3f 100644 --- a/misc/build.func +++ b/misc/build.func @@ -29,7 +29,7 @@ fi # This function enables error handling in the script by setting options and defining a trap for the ERR signal. catch_errors() { - set -Eeuo pipefail + set -Eeo pipefail trap 'error_handler $LINENO "$BASH_COMMAND"' ERR } diff --git a/misc/core.func b/misc/core.func index e71b9f9..e051b38 100644 --- a/misc/core.func +++ b/misc/core.func @@ -9,6 +9,11 @@ # } # 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 wait_for() { true @@ -38,6 +43,30 @@ load_functions() { # 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() { trap '__handle_signal_abort SIGINT' SIGINT trap '__handle_signal_abort SIGTERM' SIGTERM @@ -165,7 +194,6 @@ default_vars() { RETRY_EVERY=3 i=$RETRY_NUM #[[ "${VAR_OS:-}" == "unknown" ]] - } # ------------------------------------------------------------------------------