From 5a1c875cb828c96a08411208bbc0d90d940416f8 Mon Sep 17 00:00:00 2001 From: Lyndon Brown Date: Thu, 12 Mar 2020 18:34:36 +0000 Subject: exit: ensure an appropriate message is printed on unexpected exit if a script exits due to a failure and `set -e`, we should ensure that an error message is printed to be clear to the user that something actually went wrong. similarly it would be good to print a suitable message should the user cancel with ctrl+c for instance. Gbp-Dch: Short --- functions/exit.sh | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/functions/exit.sh b/functions/exit.sh index 737af29bc..9264f5db7 100755 --- a/functions/exit.sh +++ b/functions/exit.sh @@ -11,7 +11,7 @@ Exit () { - VALUE=$? + local VALUE=$1 if [ "${_DEBUG}" = "true" ] then @@ -63,8 +63,25 @@ Exit () return ${VALUE} } +Exit_exit () +{ + local VALUE=$? + if [ "${VALUE}" -ne 0 ]; then + Echo_error "An unexpected failure occurred, exiting..." + fi + Exit "${VALUE}" +} + +Exit_other () +{ + local VALUE=$? + Echo_warning "Unexpected early exit caught, attempting cleanup..." + Exit "${VALUE}" +} + Setup_clean_exit () { Echo_message "Setting up clean exit handler" - trap 'Exit' EXIT HUP INT QUIT TERM + trap 'Exit_other' HUP INT QUIT TERM + trap 'Exit_exit' EXIT } -- cgit v1.2.3