Skip to content

Instantly share code, notes, and snippets.

@zachriggle
Last active July 2, 2021 01:29
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 zachriggle/8574964d2e3078cdfae84b5744a9d4dc to your computer and use it in GitHub Desktop.
Save zachriggle/8574964d2e3078cdfae84b5744a9d4dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
source TRAPERR.zsh
a() {
b
}
b() {
c
}
c() {
d
}
d() {
false
}
a
#!/usr/bin/env zsh
_err() {
echo "${fg[red]}$@${reset_color}"
}
# Do not set TRAPERR if we are in an interactive shell
if [[ "$ZSH_ARGZERO" != "-zsh" ]]; then
TRAPERR() {
# If we are in a $() subshell, do not emit an error message, since
# it will just get duplicated when the result is an error code
if [ "$ZSH_SUBSHELL" -gt 0 ]; then
return
fi
# Dump the stack trace
_err "An error occurred on ${funcfiletrace[1]}"
local i=1
for trace in "${(Oa)funcfiletrace[@]}"; do
local err_file=${trace%%:*}
local err_line=${trace##*:}
_err "Frame $i (${err_file}:$err_line)"
if ! [ -f "$err_file" ]; then
true
else
awk 'NR>L-4 && NR<L+4 { printf "%-5d%4s%s\n",NR,(NR==L?">>> ":""),$0 }' "L=$err_line" "$err_file"
fi
echo
i=$(($i+1))
done >&2
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment