You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
## CONSTANTS
|
|
|
|
CURRENT_SOURCE="${BASH_SOURCE[0]}"
|
|
|
|
## FUNCTIONS
|
|
|
|
function exit {
|
|
on_exit $1 true
|
|
}
|
|
|
|
function on_exit {
|
|
local error_code bypass_last_call stack_lines
|
|
error_code=$1
|
|
|
|
bypass_last_call=${2:-false}
|
|
echo $CURRENT_SOURCE bypass_last_call=$bypass_last_call
|
|
stack_lines=("${BASH_LINENO[@]}")
|
|
|
|
#
|
|
# # remove 1 on head list
|
|
# [[ ${#stack_lines[@]} > 0 && ${stack_lines[0]} == 1 ]] && unset stack_lines[0]
|
|
#
|
|
# # remove 0 on tail list
|
|
# [[ ${#stack_lines[@]} > 0 && ${stack_lines[-1]} == 0 ]] && unset stack_lines[-1]
|
|
|
|
if [[ $error_code > 0 ]]; then
|
|
printf "\nEXIT #${error_code} due to error at line ${stack_lines[*]} : \n-----------------------------------------\n"
|
|
echo "FUNCNAME: ${FUNCNAME[@]}" >&2 # inner outer main
|
|
echo "BASH_SOURCE: ${BASH_SOURCE[@]}" >&2
|
|
echo "BASH_LINENO: ${BASH_LINENO[@]}" >&2
|
|
trap - ERR TERM INT EXIT
|
|
builtin exit "${error_code}"
|
|
else
|
|
echo SUCCESS
|
|
fi
|
|
}
|
|
|
|
function raise_error {
|
|
message=${1:-an undefined error has occured!}
|
|
code=${2:-1}
|
|
echo "error ${code}: ${message}" >&2
|
|
trap - ERR TERM INT EXIT
|
|
on_exit $code true
|
|
}
|
|
|
|
set -TEue
|
|
trap 'on_exit $?' ERR TERM INT EXIT
|