feat: new error handling

This commit is contained in:
ge
2022-10-08 21:26:08 +03:00
parent a501adfc65
commit 40fb8613be
10 changed files with 98 additions and 108 deletions

View File

@ -66,22 +66,33 @@ log() {
done
}
handle_error() {
error() {
# Handle errors
#
# Usage: handle_error ERROR_MESSAGE
# Usage: error SCRIPTNAME FUNCNAME LINENO EXIT_CODE COMMAND
# Display amd log message
log -p "$*" >&2
log -p "Check $log_file log for details." >&2
local script lastline funcname lasterr msg
script="$1"
funcname="$2"
lastline="$3"
lasterr="$4"
shift 4
# Display and log message
msg="ERROR in $script:$lastline: function '$funcname': \
command '$*' failed with exit code $lasterr"
log -p "\e[31m$msg\nCheck $log_file log for details.\e[0m" >&2
# Run user defined error handler function if set
if is_function_set on_error; then
echo -e "Run \e[1mon_error()\e[0m" | log -p
on_error "$*"
on_error "$msg"
fi
log -p "Backup ERROR: Exiting with previous errors" >&2; exit 1
log -p Exiting with previous errors. >&2
log -V Backup finished with errors.
exit 1
}
is_installed() {

View File

@ -63,17 +63,10 @@ src_mysqldump() {
log "Run command: $*"
# shellcheck disable=SC2154
if "$@" 2>> "$log_file" > "$sqldump"; then
# Compress file
sqldump="$(compress_file "$sqldump")"
# Append path to 'backups' array
backups+=("$sqldump")
log "Dump saved as: $sqldump"
else
remove_if_empty "$sqldump"
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
"$@" > "$sqldump" |& log
sqldump="$(compress_file "$sqldump")" # Compress file
backups+=("$sqldump") # Append path to 'backups' array
log "Dump saved as: $sqldump"
unset MYSQL_PWD
}

View File

@ -64,17 +64,10 @@ src_pg_dump() {
log "Run command: $*"
# shellcheck disable=SC2154
if "$@" 2>> "$log_file" > "$sqldump"; then
# Compress dump
sqldump="$(compress_file "$sqldump")"
# Append path to 'backups' array
backups+=("$sqldump")
log "Dump file: $sqldump"
else
remove_if_empty "$sqldump"
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
"$@" > "$sqldump" |& log
sqldump="$(compress_file "$sqldump")" # Compress file
backups+=("$sqldump")
log "Dump file: $sqldump"
unset PGPASSWORD
}

View File

@ -86,12 +86,7 @@ src_tar() {
log "Run command: $*"
# shellcheck disable=SC2154
if "$@" 2>> "$log_file"; then
# Append path to 'backups' array
backups+=("$archive")
else
remove_if_empty "$archive"
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
"$@" 2>> "$log_file"
# Append path to 'backups' array
backups+=("$archive")
}

View File

@ -59,12 +59,6 @@ tgt_cp() {
set -- cp --archive "$backup" "$dst_path"
log "Run command: $*"
if "$@" 2>> "$log_file"; then
: # Success
else
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
"$@" |& log
done
}

View File

@ -70,10 +70,6 @@ tgt_s3cmd() {
# Upload backups
# shellcheck disable=SC2154
if "$@" 2>> "$log_file"; then
: # Success
else
handle_error \
"Error: Something went wrong when executing command:\n\t$*"
fi
#"$@" 2>> "$log_file"
"$@" |& log
}

View File

@ -82,6 +82,7 @@ validate_targets() {
__main_target="${file_targets[0]}"
# Fail if __main_target's path is not a directory
parse_uri "$__main_target"
# shellcheck disable=SC2154
__main_target_path="$path"
# Make dirs if `make_target_dir` is set