From 266a711e192f6b5eb0b783cf95984590d8096279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Tue, 5 Dec 2023 23:21:36 +0100 Subject: [PATCH] Improve error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- padd.sh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/padd.sh b/padd.sh index 5087792..d629b78 100755 --- a/padd.sh +++ b/padd.sh @@ -102,23 +102,30 @@ padd_logo_retro_3="${bold_text}${green_text}| ${red_text}/${yellow_text}-${gre TestAPIAvailability() { - local chaos_api_list availabilityResonse + local chaos_api_list availabilityResonse cmdResult digReturnCode # Query the API URLs from FTL using CHAOS TXT # The result is a space-separated enumeration of full URLs # e.g., "http://localhost:80/api" or "https://domain.com:443/api" if [ -z "${SERVER}" ] || [ "${SERVER}" = "localhost" ] || [ "${SERVER}" = "127.0.0.1" ]; then - # --server was not set, assuming we're running locally - chaos_api_list="$(dig +short chaos txt local.api.ftl @localhost)" + # --server was not set or set to local, assuming we're running locally + cmdResult="$(dig +short chaos txt local.api.ftl @localhost 2>&1; echo $?)" else # --server was set, try to get response from there - chaos_api_list="$(dig +short chaos txt domain.api.ftl @"${SERVER}")" + cmdResult="$(dig +short chaos txt domain.api.ftl @"${SERVER}" 2>&1; echo $?)" fi - # If the query was not successful, the variable is empty - if [ -z "${chaos_api_list}" ]; then - echo "API not available. Please check connectivity" + # Gets the return code of the dig command (last line) + # We can't use${cmdResult##*$'\n'*} here as $'..' is not POSIX + digReturnCode="$(echo "${cmdResult}" | tail -n 1)" + + if [ ! "${digReturnCode}" = "0" ]; then + # If the query was not successful + echo "API not available. Please check server address and connectivity" exit 1 + else + # Dig returned 0 (success), so get the actual response (first line) + chaos_api_list="$(echo "${cmdResult}" | head -n 1)" fi # Iterate over space-separated list of URLs