Browse Source

improve error handling for [run-command-as] helper

Christian Winther 1 year ago
parent
commit
5a43d7a65d
1 changed files with 9 additions and 0 deletions
  1. 9 0
      docker/shared/root/docker/helpers.sh

+ 9 - 0
docker/shared/root/docker/helpers.sh

@@ -91,20 +91,29 @@ function run-command-as()
 
 
     log-info-stderr "${notice_message_color}👷 Running [${*}] as [${target_user}]${color_clear}"
     log-info-stderr "${notice_message_color}👷 Running [${*}] as [${target_user}]${color_clear}"
 
 
+    # disable error on exit behavior temporarily while we run the command
+    set +e
+
     if [[ ${target_user} != "root" ]]; then
     if [[ ${target_user} != "root" ]]; then
         stream-prefix-command-output su --preserve-environment "${target_user}" --shell /bin/bash --command "${*}"
         stream-prefix-command-output su --preserve-environment "${target_user}" --shell /bin/bash --command "${*}"
     else
     else
         stream-prefix-command-output "${@}"
         stream-prefix-command-output "${@}"
     fi
     fi
 
 
+    # capture exit code
     exit_code=$?
     exit_code=$?
 
 
+    # re-enable exit code handling
+    set -e
+
     if [[ $exit_code != 0 ]]; then
     if [[ $exit_code != 0 ]]; then
         log-error "${error_message_color}❌ Error!${color_clear}"
         log-error "${error_message_color}❌ Error!${color_clear}"
+
         return "$exit_code"
         return "$exit_code"
     fi
     fi
 
 
     log-info-stderr "${success_message_color}✅ OK!${color_clear}"
     log-info-stderr "${success_message_color}✅ OK!${color_clear}"
+
     return "$exit_code"
     return "$exit_code"
 }
 }