Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivanstepanovftw/88a0510f79df83e863320d2875ab3145 to your computer and use it in GitHub Desktop.
Save ivanstepanovftw/88a0510f79df83e863320d2875ab3145 to your computer and use it in GitHub Desktop.
Bash Error & Backtrace Function

Bash Error & Backtrace Function

./error.sh

variable="$(echo INJECTED | base64)"
variable='$(echo '"$variable"' | base64 --decode)'
error
error "$variable     foo"
error '  foo"bar  ' $'\n''\nbaz)'
./build.sh:72 (main): Error: no message
./build.sh:73 (main): Error: $(echo SU5KRUNURUQ= | base64 --decode)     spaces
./build.sh:74 (main): Error:   foo"bar
\nbaz)
./build.sh:75 (main): exited with return code 1.
Error: no message
./build.sh:76 (main): exited with return code 1.
Error: command 'false' is failed

./error_v2.sh

variable="$(echo "INJECTED" | base64)"
variable='$(echo '"$variable"' | base64 --decode);'
error
error '   foo"bar  ' $'\n''\nbaz)' "$variable"
false || error
false || error "command 'false' failed"

find_a() {
  _test_path() {
    false || error
    false || error "command 'false' is broken"
  }
  _test_path
}
find_a
Error: no message
Script version: 1.0

---
Error:    foo"bar   
\nbaz) $(echo SU5KRUNURUQK | base64 --decode);
Script version: 1.0

---
Error: no message
Script version: 1.0
Backtrace (1):
  ./build.sh:73 main [return code 1]
---
Error: command 'false' failed
Script version: 1.0
Backtrace (1):
  ./build.sh:74 main [return code 1]
---
Error: no message
Script version: 1.0
Backtrace (3):
  ./build.sh:83 main
  ./build.sh:81 find_a
  ./build.sh:78 _test_path [return code 1]
---
Error: command 'false' is broken
Script version: 1.0
Backtrace (3):
  ./build.sh:83 main
  ./build.sh:81 find_a
  ./build.sh:79 _test_path [return code 1]
shopt -s expand_aliases
alias error='_error "${LINENO:-0}" "${FUNCNAME[*]:-main}"'
function _error() {
local return_code=$?
local lineno=${1:-0}
local funcname=${2:-main}
shift 2
local message=${*:-"no message"}
message="Error: ${message}"
local message_rc
[[ $return_code != 0 ]] && message_rc="exited with return code ${return_code}.\n"
printf "%s:%d (%s): ${message_rc}%s\n" "${BASH_SOURCE[0]}" "${lineno}" "${funcname}" "${message}"
exit 1
}
ROOT_DIR=$( (cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P) )
SCRIPT_VERSION="1.0"
# check if stdout is a terminal
if test -t 1; then
# see if it supports colors
ncolors=$(tput colors 2>/dev/null)
if [[ -n $ncolors && $ncolors -ge 8 ]]; then
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi
function backtrace() {
local strip="${1:-'1'}"
echo "Backtrace ($((${#FUNCNAME[@]}-${strip}))):";
for ((i="$((${#FUNCNAME[@]}-1))"; i>="${strip}"; i--)); do
echo " ${BASH_SOURCE[$i]}:${BASH_LINENO[$i-1]} ${FUNCNAME[$i]}";
done
}
function error() {
local return_code=$?
local message="$*"
local version=$'\n'"Script version: $SCRIPT_VERSION"
local trace
message=${message:-"no message"}
[[ $return_code != 0 ]] && trace=$'\n'"$(backtrace 2) [return code ${return_code}]"
echo "${red}Error: ${message}${version}${trace}${normal}"
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment