From 997eb0be1b0c0934b363e971cb7e9c5988e0841a Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Tue, 3 Jul 2018 20:48:30 -0400 Subject: [PATCH] nicer names, update copyright --- README | 9 +++-- err | 40 +++++++++++++------ errallow-function => err-allow-function | 4 +- ...-trace-function => err-bash-trace-function | 4 +- errcatch-function => err-catch-function | 6 +-- errexit-function => err-exit-function | 8 ++-- gen-err | 10 ----- generate-err | 38 ++++++++++++++++++ 8 files changed, 81 insertions(+), 38 deletions(-) rename errallow-function => err-allow-function (93%) rename bash-trace-function => err-bash-trace-function (96%) rename errcatch-function => err-catch-function (93%) rename errexit-function => err-exit-function (88%) delete mode 100755 gen-err create mode 100755 generate-err diff --git a/README b/README index 180c188..ff45665 100644 --- a/README +++ b/README @@ -1,10 +1,11 @@ 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. diff --git a/err b/err index 57ea542..70dd6b2 100644 --- a/err +++ b/err @@ -1,6 +1,27 @@ #!/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 @@ -25,21 +46,14 @@ bash-trace() { 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" @@ -48,7 +62,7 @@ errcatch() { trap _err-trap ERR set -o pipefail } -errexit() { +err-exit() { exec >&2 code=1 if [[ $@ ]]; then @@ -62,8 +76,8 @@ errexit() { 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 diff --git a/errallow-function b/err-allow-function similarity index 93% rename from errallow-function rename to err-allow-function index aff8593..265c838 100644 --- a/errallow-function +++ b/err-allow-function @@ -1,5 +1,5 @@ #!/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. @@ -13,7 +13,7 @@ # 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 diff --git a/bash-trace-function b/err-bash-trace-function similarity index 96% rename from bash-trace-function rename to err-bash-trace-function index 6f677aa..f46e165 100644 --- a/bash-trace-function +++ b/err-bash-trace-function @@ -1,5 +1,5 @@ #!/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. @@ -17,7 +17,7 @@ # 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 diff --git a/errcatch-function b/err-catch-function similarity index 93% rename from errcatch-function rename to err-catch-function index 4fb8c37..ccb2145 100644 --- a/errcatch-function +++ b/err-catch-function @@ -1,5 +1,5 @@ #!/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. @@ -18,14 +18,14 @@ # 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" diff --git a/errexit-function b/err-exit-function similarity index 88% rename from errexit-function rename to err-exit-function index cbe2118..e72f2f5 100644 --- a/errexit-function +++ b/err-exit-function @@ -1,5 +1,5 @@ #!/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. @@ -13,8 +13,8 @@ # 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 @@ -28,7 +28,7 @@ errexit() { fi fi echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}" - bash-trace 2 + err-bash-trace 2 echo "$0: exiting with code $code" exit $err } diff --git a/gen-err b/gen-err deleted file mode 100755 index 236c3a7..0000000 --- a/gen-err +++ /dev/null @@ -1,10 +0,0 @@ -#!/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 diff --git a/generate-err b/generate-err new file mode 100755 index 0000000..069e693 --- /dev/null +++ b/generate-err @@ -0,0 +1,38 @@ +#!/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 -- 2.25.1