error_handler

This commit is contained in:
CanbiZ 2025-09-15 15:10:00 +02:00
parent 569384b5d0
commit 837108265a
4 changed files with 104 additions and 43 deletions

View File

@ -21,19 +21,41 @@ verb_ip6() {
fi
}
# This function catches errors and handles them with the error handler function
catch_errors() {
set -Eeuo pipefail
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 handles errors
error_handler() {
on_exit() {
local exit_code="$?"
local line_number="$1"
local command="$2"
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
echo -e "\n$error_message\n"
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
exit "$exit_code"
}
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
}
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection

View File

@ -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

View File

@ -25,34 +25,42 @@ fi
# Strict error handling
# ------------------------------------------------------------------------------
set -Eeuo pipefail
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
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"
}
on_exit() {
local exit_code="$?"
[[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile"
exit "$exit_code"
}
error_handler() {
local exit_code="$?"
local line_number="$1"
local command="$2"
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"
}
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
}
exit_script() {
clear
printf "\e[?25h"

View File

@ -576,5 +576,29 @@ if [ "$START_VM" == "yes" ]; then
msg_ok "Started Debian 13 VM"
fi
msg_info "Installing resize tools in VM"
qm set "$VMID" --sshkeys ~/.ssh/id_rsa.pub >/dev/null 2>&1 || true
qm exec "$VMID" -- bash -c "apt-get update && apt-get install -y cloud-guest-utils e2fsprogs xfsprogs"
msg_ok "Installed resize tools in VM"
msg_info "Injecting auto-resize script into VM"
cat <<'EOF' >/var/lib/vz/snippets/resize-root.sh
#!/bin/bash
set -e
DEVICE=$(lsblk -no pkname $(findmnt -n -o SOURCE /))
PART=$(findmnt -n -o SOURCE / | sed 's|/dev/||')
growpart /dev/${DEVICE} ${PART##*[a-z]}
FSTYPE=$(findmnt -n -o FSTYPE /)
if [ "$FSTYPE" = "ext4" ]; then
resize2fs /dev/$PART
elif [ "$FSTYPE" = "xfs" ]; then
xfs_growfs /
fi
EOF
chmod +x /var/lib/vz/snippets/resize-root.sh
qm set "$VMID" --hookscript local:snippets/resize-root.sh >/dev/null
msg_ok "Injected auto-resize script"
msg_ok "Completed Successfully!\n"
echo "More Info at https://github.com/community-scripts/ProxmoxVE/discussions/836"