feat: Add error handling

This commit is contained in:
ge 2022-05-15 07:19:12 +03:00
parent af6f60c8f5
commit c43346aa35

View File

@ -30,24 +30,91 @@ log() {
log_file="$__log_file" log_file="$__log_file"
# If no arguments passed OR '0' descriptor is open, then read from descriptor. # Read message from STDIN if args not passed.
# If no arguments AND '0' descriptor is closed, then 'message' will not be set. [[ ! -t 0 ]] || [[ "$#" == 0 ]] && message="$(cat <&0)"
[[ "$#" == 0 || -t 0 ]] || message="$(cat <&0)"
# Set log date format # Set log date format
[ -n "$log_date_fmt" ] || log_date_fmt='%d/%b/%Y:%H:%M:%S %z' [ "$log_date_fmt" ] || log_date_fmt='%d/%b/%Y:%H:%M:%S %z'
while (( "$#" )); do while (( "$#" )); do
case "$1" in case "$1" in
-p|--print) print=1; shift;; -p) print=1; shift;;
-) message="$(cat <&0)"; shift "$#";; -) message="$(cat <&0)"; shift "$#";;
*) message="$@"; shift "$#";; *) message="$*"; shift "$#";;
esac esac
done done
[[ "$print" == 1 ]] && echo "$message" [[ "$print" == 1 ]] && echo -e "$message"
while read line; do while read -r line; do
if [[ "$line" ]]; then
printf '[%s] [%s] %s\n' "$(date +"$log_date_fmt")" "$0" "$line" >> "$log_file" printf '[%s] [%s] %s\n' "$(date +"$log_date_fmt")" "$0" "$line" >> "$log_file"
fi
done <<< "$(sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g' <<< "$message")" done <<< "$(sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g' <<< "$message")"
} }
err() {
# Handle error. Print errors to STDERR, log, process
# and exit from script.
#
# Usage: err [-eao] MESSAGE
local message
local errexit
local errappend
local onerr
[[ ! -t 0 ]] || [[ "$#" == 0 ]] && message="$(cat <&0)"
# Split combined short options, e.g. '-abc' to '-a' '-b' '-c'
for args in "$@"; do
shift
case "$args" in
-*)
args="$(echo "${args:1}" | grep -o . | xargs -I {} echo -n '-{} ')"
set -- "$@" $args;;
*)
set -- "$@" "$args";; # save positional arguments
esac
done
while (( "$#" )); do
case "$1" in
-e) errexit=1; shift;;
-a) errappend=1; shift;;
-o) onerr=1; shift;;
-) message="$(cat <&0)"; shift "$#";;
*) message="$*"; shift "$#";;
esac
done
log "$message"
echo -e "$message" >&2
[ "$errappend" ] && errors+=("$message")
# Run user defined function on_error()
if [ "$onerr" ]; then
if is_function_set on_error; then
on_error
fi
fi
# Exit
if [ "$errexit" ]; then
log "Backup ERROR: Exiting with previous errors"; exit 1
fi
}
try() {
# Commands wrapper for error handling
#
# Usage: try COMMAND
if eval "$@" 2>> "$__log_file"; then
return 0 # Success
else
err -a "Error: Something went wrong when executing command:"
err -a " $*"
err -aeo "Check $__log_file log for details."
fi
}