mirror of
https://github.com/community-scripts/ProxmoxVED.git
synced 2026-02-25 05:57:26 +00:00
error_handler
This commit is contained in:
@@ -28,34 +28,41 @@ elif command -v wget >/dev/null 2>&1; then
|
||||
#echo "(build.func) Loaded core.func via wget"
|
||||
fi
|
||||
|
||||
# This function enables error handling in the script by setting options and defining a trap for the ERR signal.
|
||||
catch_errors() {
|
||||
set -Eeo pipefail
|
||||
# if [ -n "$BASH_VERSION" ] && command -v shopt >/dev/null 2>&1; then
|
||||
# shopt -s errtrace
|
||||
# fi
|
||||
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
|
||||
set -Eeuo pipefail
|
||||
trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR
|
||||
trap on_exit EXIT
|
||||
trap on_interrupt INT
|
||||
trap on_terminate TERM
|
||||
|
||||
error_handler() {
|
||||
local exit_code="$1"
|
||||
local line_number="$2"
|
||||
local command="$3"
|
||||
|
||||
# Exitcode 0 = kein Fehler → ignorieren
|
||||
if [[ "$exit_code" -eq 0 ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
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"
|
||||
exit "$exit_code"
|
||||
}
|
||||
|
||||
# This function is called when an error occurs. It receives the exit code, line number, and command that caused the error, and displays an error message.
|
||||
error_handler() {
|
||||
on_exit() {
|
||||
local exit_code="$?"
|
||||
local line_number="$1"
|
||||
local command="$2"
|
||||
printf "\e[?25h"
|
||||
local error_message="[ERROR] in line $line_number: exit code $exit_code: while executing command $command"
|
||||
post_update_to_api "failed" "$command"
|
||||
echo -e "\n$error_message\n"
|
||||
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
|
||||
exit "$exit_code"
|
||||
}
|
||||
|
||||
if [[ -n "$CT_ID" ]]; then
|
||||
read -p "Remove this Container? <y/N> " prompt
|
||||
if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
|
||||
pct stop "$CT_ID" &>/dev/null
|
||||
pct destroy "$CT_ID" &>/dev/null
|
||||
msg_ok "Removed this Container"
|
||||
fi
|
||||
fi
|
||||
on_interrupt() {
|
||||
echo -e "\n${RD}Interrupted by user (SIGINT)${CL}"
|
||||
exit 130
|
||||
}
|
||||
|
||||
on_terminate() {
|
||||
echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}"
|
||||
exit 143
|
||||
}
|
||||
|
||||
# Check if the shell is using bash
|
||||
|
||||
Reference in New Issue
Block a user