Functions to deal with errors in bash
-err: usually, just source this. contains the others and runs errcatch
+err: usually, just copy and source this. contains the other files
-errcatch: set a trap on error to do bash-trace and exit
-errallow: undo errcatch
-errexit: exit and print stack trace
+
+err-catch: set a trap on error to do bash-trace and exit
+err-allow: undo errcatch
+err-exit: exit and print stack trace
bash-trace: print stack trace (best bash stack trace ever)
the err* functions depend on bash-trace.
#!/bin/bash
+# Copyright 2018 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
# this file was generated from gen-err and meant to be sourced
-bash-trace() {
+err-allow() {
+ if [[ $1 ]]; then
+ echo "errallow help: Undo the complimentary errcatch function."
+ else
+ set +E +o pipefail; trap ERR
+ fi
+}
+err-bash-trace() {
local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
local source
local extdebug=false
echo \'
done
}
-errallow() {
- if [[ $1 ]]; then
- echo "errallow help: Undo the complimentary errcatch function."
- else
- set +E +o pipefail; trap ERR
- fi
-}
-errcatch() {
+err-catch() {
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
+ err-bash-trace 2
set -e
"${_errcatch_cleanup[@]}"
echo "$0: exiting with code $err"
trap _err-trap ERR
set -o pipefail
}
-errexit() {
+err-exit() {
exec >&2
code=1
if [[ $@ ]]; then
fi
fi
echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
- bash-trace 2
+ err-bash-trace 2
echo "$0: exiting with code $code"
exit $err
}
-errcatch
+err-catch-function
#!/bin/bash
-# Copyright (C) 2014 Ian Kelling
+# Copyright (C) 2018 Ian Kelling
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# See the License for the specific language governing permissions and
# limitations under the License.
-errallow() {
+err-allow() {
if [[ $1 ]]; then
echo "errallow help: Undo the complimentary errcatch function."
else
#!/bin/bash
-# Copyright (C) 2015 Ian Kelling
+# Copyright (C) 2018 Ian Kelling
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# shopt -s extdebug
# Also note, setting extdebug in your .bashrc causes spam in bash 4.4.
-bash-trace() {
+err-bash-trace() {
local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
local source
local extdebug=false
#!/bin/bash
-# Copyright (C) 2014 Ian Kelling
+# Copyright (C) 2018 Ian Kelling
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# While running that, we exit with set -e, no stack trace, as
# err trap does not work within an error trap
-errcatch() {
+err-catch() {
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
+ err-bash-trace 2
set -e
"${_errcatch_cleanup[@]}"
echo "$0: exiting with code $err"
#!/bin/bash
-# Copyright (C) 2015 Ian Kelling
+# Copyright (C) 2018 Ian Kelling
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# See the License for the specific language governing permissions and
# limitations under the License.
-errexit() {
- # usage: errexit [EXIT_CODE] [MESSAGE]
+err-exit() {
+ # usage: err-exit [EXIT_CODE] [MESSAGE]
exec >&2
code=1
if [[ $@ ]]; then
fi
fi
echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
- bash-trace 2
+ err-bash-trace 2
echo "$0: exiting with code $code"
exit $err
}
+++ /dev/null
-#!/bin/bash
-
-x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
-
-cat <<'EOF' >err
-#!/bin/bash
-# this file was generated from gen-err and meant to be sourced
-EOF
-sed -r '/^ *(#|$)/d' *-function >>err
-echo errcatch >>err
--- /dev/null
+#!/bin/bash
+# Copyright 2018 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*} # directory of this file
+
+cat <<'EOF' >err
+#!/bin/bash
+# Copyright 2018 Ian Kelling
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# this file was generated from generate-err and meant to be sourced
+EOF
+sed -r '/^ *(#|$)/d' *-function >>err
+echo err-catch >>err