summaryrefslogtreecommitdiff
path: root/include/err.sh
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2024-03-27 01:19:39 +0000
committerLeah Rowe <leah@libreboot.org>2024-03-27 01:50:31 +0000
commit6ebab10caa5be6fc1cfd244e745851687d4bd70d (patch)
tree0b983459c0cbc1b8998186975f9a571decb05592 /include/err.sh
parent6b11f1b055466982747f65665506173a0b22557b (diff)
safer, simpler error handling in lbmk
in shell scripts, a function named the same as a program included in the $PATH will override that program. for example, you could make a function called ls() and this would override the standand "ls". in lbmk, a part of it was first trying to run the "fail" command, deferring to "err", because some scripts call fail() which does some minor cleanup before calling err. in most cases, fail() is not defined, and it's possible that the user could have a program called "fail" in their $PATH, the behaviour of which we could not determine, and it could have disastrous effects. lbmk error handling has been re-engineered in such a way that the err function is defined in a variable, which defaults to err_ which calls err_, so defined under include/err.sh. in functions that require cleanup prior to error handling, a fail() function is still defined, and err is overridden, thus: err="fail" this change has made xx_() obsolete, so now only x_ is used. the x_ function is a wrapper that can be used to run a command and exit with non-zero status (from lbmk) if the command fails. the xx_ command did the same thing, but called fail() which would have called err(); now everything is $err example: rm -f "$filename" || err "could not delete file" this would now be: rm -f "$filename" || $err "could not delete file" overriding of err= must be done *after* including err.sh. for example: err="fail" . "include/err.sh" ^ this is wrong. instead, one must do: . "include/err.sh" err="fail" this is because err is set as a global variable under err.sh the new error handling is much cleaner, and safer. it also reduces the chance of mistakes such as: calling err when you meant to call fail. this is because the standard way is now to call $err, so you set err="fail" at the top of the script and all is well. Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'include/err.sh')
-rwxr-xr-xinclude/err.sh25
1 files changed, 7 insertions, 18 deletions
diff --git a/include/err.sh b/include/err.sh
index cab81d02..5b66c999 100755
--- a/include/err.sh
+++ b/include/err.sh
@@ -2,19 +2,10 @@
# SPDX-FileCopyrightText: 2022, 2023 Leah Rowe <leah@libreboot.org>
version=""; versiondate=""; projectname=""; _nogit=""
+err="err_"
x_() {
- [ $# -lt 1 ] || ${@} || err_exit err ${@}
-}
-xx_() {
- [ $# -lt 1 ] || ${@} || err_exit fail ${@}
-}
-
-err_exit()
-{
- _fail="${1}" && shift 1
- echo "Non-zero exit: $@"
- $_fail "Unhandled error"
+ [ $# -lt 1 ] || ${@} || $err "Unhandled non-zero exit: $@"; return 0
}
check_git()
@@ -30,8 +21,7 @@ check_git()
git_err()
{
printf "You need to set git name/email, like so:\n%s\n\n" "$1" 1>&2
- fail "Git name/email not configured" || \
- err "Git name/email not configured"
+ $err "Git name/email not configured"
}
check_project()
@@ -49,9 +39,8 @@ check_project()
--pretty='%ct' HEAD)" || versiondate="${versiondate_}"
for p in projectname version versiondate; do
- eval "[ -n \"\$$p\" ] || fail \"$p unset\" || err \"$p unset\""
- p_="x_ printf \"%s\\n\" \"\$$p\" > $p"
- eval "x$p_ || $p_"
+ eval "[ -n \"\$$p\" ] || $err \"$p unset\""
+ eval "x_ printf \"%s\\n\" \"\$$p\" > $p"
done
export LOCALVERSION="-${projectname}-${version%%-*}"
}
@@ -59,7 +48,7 @@ check_project()
setvars()
{
_setvars=""
- [ $# -lt 2 ] && err "setvars: too few arguments"
+ [ $# -lt 2 ] && $err "setvars: too few arguments"
val="${1}" && shift 1
for var in $@; do
_setvars="${var}=\"${val}\"; ${_setvars}"
@@ -67,7 +56,7 @@ setvars()
printf "%s\n" "${_setvars% }"
}
-err()
+err_()
{
printf "ERROR %s: %s\n" "${0}" "${1}" 1>&2
exit 1