# 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