Update "error tracing" doc (#2216)

This commit is contained in:
Casper 2021-09-26 10:37:50 +02:00 committed by GitHub
parent 23b8523ee3
commit 054ee5bd09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -87,17 +87,20 @@ A construct to trace error in your scripts looks like this. Remember: Remove `se
```bash ```bash
set -xeuEo pipefail set -xeuEo pipefail
trap '__log_err ${FUNCNAME[0]:-"?"} ${BASH_COMMAND:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR trap '__err "${BASH_SOURCE}" "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" "${LINENO:-?}" "${?:-?}"' ERR
SCRIPT='name_of_this_script.sh' function __err
function __log_err
{ {
printf "\n--- \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \ local RED="\e[31m\e[1m"
" - script = ${SCRIPT:-${0}}" \ local RESET="\e[0m"
" - function = ${1} / ${2}" \ local ERR_MSG="\n--- ${RED}UNCHECKED ERROR${RESET}"
" - line = ${3}" \ ERR_MSG+="\n - script = ${1}"
" - exit code = ${4}" 1>&2 ERR_MSG+="\n - function = ${2}"
ERR_MSG+="\n - command = ${3}"
ERR_MSG+="\n - line = ${4}"
ERR_MSG+="\n - exit code = ${5}"
echo -e "${ERR_MSG}"
<CODE TO RUN AFTERWARDS> <CODE TO RUN AFTERWARDS>
} }