Skip to content

Instantly share code, notes, and snippets.

@jcttrll
Created February 29, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcttrll/a5d195a80daec2302cded2d05c3882b3 to your computer and use it in GitHub Desktop.
Save jcttrll/a5d195a80daec2302cded2d05c3882b3 to your computer and use it in GitHub Desktop.
Bash error handler, with stacktrace with clickable file:line links (in IntelliJ)
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