Commit | Line | Data |
---|---|---|
78a1a75c | 1 | #!/bin/bash |
997eb0be IK |
2 | # Copyright 2018 Ian Kelling |
3 | ||
4 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | # you may not use this file except in compliance with the License. | |
6 | # You may obtain a copy of the License at | |
7 | ||
8 | # http://www.apache.org/licenses/LICENSE-2.0 | |
9 | ||
10 | # Unless required by applicable law or agreed to in writing, software | |
11 | # distributed under the License is distributed on an "AS IS" BASIS, | |
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | # See the License for the specific language governing permissions and | |
14 | # limitations under the License. | |
15 | ||
78a1a75c | 16 | # this file was generated from gen-err and meant to be sourced |
997eb0be IK |
17 | err-allow() { |
18 | if [[ $1 ]]; then | |
19 | echo "errallow help: Undo the complimentary errcatch function." | |
20 | else | |
21 | set +E +o pipefail; trap ERR | |
22 | fi | |
23 | } | |
24 | err-bash-trace() { | |
78a1a75c IK |
25 | local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent |
26 | local source | |
27 | local extdebug=false | |
28 | if [[ $(shopt -p extdebug) == *-s* ]]; then | |
29 | extdebug=true | |
30 | fi | |
31 | for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do | |
32 | argc=${BASH_ARGC[frame]} | |
33 | argc_index+=$argc | |
34 | ((frame < start)) && continue | |
35 | if (( ${#BASH_SOURCE[@]} > 1 )); then | |
36 | source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:" | |
37 | fi | |
38 | indent=$((frame-start+1)) | |
39 | indent=$((indent < max_indent ? indent : max_indent)) | |
40 | printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}" | |
41 | if $extdebug; then | |
42 | for ((i=argc_index-1; i >= argc_index-argc; i--)); do | |
43 | printf " %s" "${BASH_ARGV[i]}" | |
44 | done | |
45 | fi | |
46 | echo \' | |
47 | done | |
48 | } | |
997eb0be | 49 | err-catch() { |
78a1a75c IK |
50 | set -E; shopt -s extdebug |
51 | _err-trap() { | |
52 | err=$? | |
53 | exec >&2 | |
54 | set +x | |
55 | echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err" | |
997eb0be | 56 | err-bash-trace 2 |
78a1a75c IK |
57 | set -e |
58 | "${_errcatch_cleanup[@]}" | |
59 | echo "$0: exiting with code $err" | |
60 | exit $err | |
61 | } | |
62 | trap _err-trap ERR | |
63 | set -o pipefail | |
64 | } | |
997eb0be | 65 | err-exit() { |
78a1a75c IK |
66 | exec >&2 |
67 | code=1 | |
68 | if [[ $@ ]]; then | |
69 | if [[ ${1/[^0-9]/} == "$1" ]]; then | |
70 | code=$1 | |
71 | if [[ $2 ]]; then | |
72 | echo "$2" | |
73 | fi | |
74 | else | |
75 | echo "$0: $1" | |
76 | fi | |
77 | fi | |
78 | echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}" | |
997eb0be | 79 | err-bash-trace 2 |
78a1a75c IK |
80 | echo "$0: exiting with code $code" |
81 | exit $err | |
82 | } | |
997eb0be | 83 | err-catch-function |