aboutsummaryrefslogtreecommitdiff
path: root/err
blob: 57ea5421d50fa5a7a9779dea64c7f98a76b5e94a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# this file was generated from gen-err and meant to be sourced
bash-trace() {
    local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
    local source
    local extdebug=false
    if [[ $(shopt -p extdebug) == *-s* ]]; then
        extdebug=true
    fi
    for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
        argc=${BASH_ARGC[frame]}
        argc_index+=$argc
        ((frame < start)) && continue
        if (( ${#BASH_SOURCE[@]} > 1 )); then
            source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
        fi
        indent=$((frame-start+1))
        indent=$((indent < max_indent ? indent : max_indent))
        printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
        if $extdebug; then
            for ((i=argc_index-1; i >= argc_index-argc; i--)); do
                printf " %s" "${BASH_ARGV[i]}"
            done
        fi
        echo \'
    done
}
errallow() {
    if [[ $1 ]]; then
        echo "errallow help: Undo the complimentary errcatch function."
    else
        set +E +o pipefail; trap ERR
    fi
}
errcatch() {
    set -E; shopt -s extdebug
    _err-trap() {
        err=$?
        exec >&2
        set +x
        echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
        bash-trace 2
        set -e
        "${_errcatch_cleanup[@]}"
        echo "$0: exiting with code $err"
        exit $err
    }
    trap _err-trap ERR
    set -o pipefail
}
errexit() {
    exec >&2
    code=1
    if [[ $@ ]]; then
        if [[ ${1/[^0-9]/} == "$1" ]]; then
            code=$1
            if [[ $2 ]]; then
                echo "$2"
            fi
        else
            echo "$0: $1"
        fi
    fi
    echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
    bash-trace 2
    echo "$0: exiting with code $code"
    exit $err
}
errcatch