preliminary support for error management

This commit is contained in:
fatalerrors
2021-06-17 18:28:03 +02:00
parent bf694f7c57
commit 71a283e0f5
2 changed files with 131 additions and 1 deletions

11
init.sh
View File

@@ -2,6 +2,15 @@
# Init : initialise une machine et la met en conformité
# Copyright (c) 2021 Geoffray Levasseur <geoffray.levasseur@obs-mip.fr>
# trace ERR through pipes
set -o pipefail
# trace ERR through 'time command' and other functions
set -o errtrace
# set -e : exit the script if any statement returns a non-true return value
set -o errexit
export VERSION="0.95.1"
# Stocke le chemin du script
@@ -120,5 +129,5 @@ install_mk-agent
conf_syslog
conf_mail
echo "That's all folks !"
prnt I "That's all folks !"
echo "Après vérification des logs, il est recommandé de redémarrer la machine..."

121
lib/aaa_errors.sh Normal file
View File

@@ -0,0 +1,121 @@
# Error management functions
function exit_handler()
{
local error_code="$?"
[[ $error_code == 0 ]] && return;
# Local variables
local i=0
local regex=''
local mem=''
local error_file=''
local error_lineno=''
local lineno=''
# Print error header
prnt E "${BIWhite}*** ${BIRed}Une erreur fatale est intervenue, \
le script va s'arrêter prématurément !$DEFAULTCOL"
# Getting backtrace
_backtrace=$( backtrace 2 )
# MANAGING THE OUTPUT:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
regex='^([a-z]{1,}) ([0-9]{1,})$'
if [[ $error_lineno =~ $regex ]]; then
# The error line was found on the log
# (e.g. type 'ff' without quotes wherever)
# --------------------------------------------------------------
local row="${BASH_REMATCH[1]}"
lineno="${BASH_REMATCH[2]}"
echo -e "Fichier :\t\t${error_file}"
echo -e "${row} :\t\t${lineno}\n"
echo -e "Code d'erreur :\t${error_code}"
else
regex="^${error_file}\$|^${error_file}\s+|\s+${error_file}\s+|\s+${error_file}\$"
if [[ "$_backtrace" =~ $regex ]]; then
# The file was found on the log but not the error line
# (could not reproduce this case so far)
# ------------------------------------------------------
echo -e "Fichier :\t\t$error_file"
echo -e "Code d'ereur :\t${error_code}"
else
# The error file is the first on backtrace list:
# Exploding backtrace on newlines
mem=$IFS
IFS="\n"
IFS=${IFS:0:1}
local lines=( $_backtrace )
IFS=$mem
error_file=""
if [[ -n "${lines[1]}" ]]; then
array=( ${lines[1]} )
for (( i=2; i<${#array[@]}; i++ )); do
error_file="$error_file ${array[$i]}"
done
# Trim
error_file="$( echo "$error_file" |
sed -e 's/^[ \t]*//' |
sed -e 's/[ \t]*$//' )"
fi
echo -e "Fichier :\t\t$error_file"
echo -e "Colonne :\t\tunknown\n"
echo -e "Code d'erreur :\t${error_code}"
fi
fi
# Print the backtrace
echo -e "\n$_backtrace\n"
# Exit the script
prnt E "Sortie !"
exit "$error_code"
}
trap exit_handler EXIT # ! ! ! TRAP EXIT ! ! !
trap exit ERR # ! ! ! TRAP ERR ! ! !
# Print a backtrace
function backtrace
{
local _start_from_=0
local params=( "$@" )
if (( "${#params[@]}" >= "1" )); then
_start_from_="$1"
fi
local i=0
local first=false
local last=false
while caller $i > /dev/null; do
if [[ -n "$_start_from_" ]] && (( "$i" + 1 >= "$_start_from_" )); then
if [[ "$first" == false ]]; then
echo "==== Pile d'appel ===="
first=true
fi
caller $i
fi
(( i=i++ ))
done
}
export -f backtrace