feat: new error handling
This commit is contained in:
@ -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() {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user