From 59a46b37545a0ba652498c3a64d467a868f23072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=83rcziem=20=E2=84=A2?= <118485377+spalencsar@users.noreply.github.com> Date: Fri, 3 Oct 2025 11:43:47 +0200 Subject: [PATCH] fix(handle_error): support wrapper-call usage and trap handler --- setup.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/setup.sh b/setup.sh index 6063a3d..2e0e730 100644 --- a/setup.sh +++ b/setup.sh @@ -58,16 +58,35 @@ exec > >(tee -a "${LOG_FILE}") 2>&1 # Enhanced error handling with rollback handle_error() { + # Dual-mode: if called with a non-numeric first argument, treat as wrapper + # e.g. handle_error sudo cp ... -> execute the command and handle failures + if [[ $# -ge 1 && ! "$1" =~ ^[0-9]+$ ]]; then + "$@" + local _rc=$? + if [[ $_rc -ne 0 ]]; then + local _line_number=${BASH_LINENO[0]:-0} + local _command="$*" + log_error "Script failed at line ${_line_number}: ${_command} (exit code: ${_rc})" + if ask_yes_no "An error occurred. Would you like to rollback changes?" "y"; then + execute_rollback + fi + cleanup + exit $_rc + fi + return 0 + fi + + # Trap handler mode: handle_error "" local exit_code=$? local line_number=$1 local command="$2" - + log_error "Script failed at line ${line_number}: ${command} (exit code: ${exit_code})" - + if ask_yes_no "An error occurred. Would you like to rollback changes?" "y"; then execute_rollback fi - + cleanup exit $exit_code }