Created
February 29, 2020 13:22
-
-
Save jcttrll/a5d195a80daec2302cded2d05c3882b3 to your computer and use it in GitHub Desktop.
Bash error handler, with stacktrace with clickable file:line links (in IntelliJ)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trap onError ERR | |
onError() { | |
local exitCode=$? command=(${BASH_COMMAND}) | |
if [[ ${BASH_SUBSHELL} -gt 0 ]]; then | |
exit ${exitCode} | |
fi | |
echo -e "\nERROR: Failed with exit code $exitCode when executing: ${command[@]}" >&2 | |
local i j | |
for ((i = 1; i < ${#FUNCNAME[@]} - 1; i++)); do | |
let j=i-1 || true | |
echo " at .${FUNCNAME[i]}(${BASH_SOURCE[i]#./}:${BASH_LINENO[j]})" >&2 | |
done | |
exit ${exitCode} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment