Skip to content

Commit 0ff0b96

Browse files
committed
more debug messages, fail if the response contains error key
1 parent 3ba8a36 commit 0ff0b96

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

ephemeral/retry-function.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ retry() {
1919
done
2020
echo "Command failed after $retries retries." >&2
2121
return 1
22+
}
23+
24+
# Helper function to check for a JSON error response from the API
25+
# Usage: check_for_api_error "<response_body>" "<context_message>"
26+
check_for_api_error() {
27+
local response="$1"
28+
local context_message="$2"
29+
if echo "$response" | jq -e 'if type == "object" and has("error") then true else false end' > /dev/null; then
30+
echo "API error during '$context_message': $response" >&2
31+
return 1
32+
fi
33+
return 0
2234
}

ephemeral/shutdown/action.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,29 @@ runs:
4343
shutdown_instance() {
4444
# The API returns a 200 on successful deletion.
4545
# We use --fail-with-body so curl fails on server errors (5xx) and triggers the retry.
46-
# We allow a 404 since that means the instance is already gone.
47-
local http_code
48-
http_code=$(curl --fail-with-body -s -o /dev/null -w "%{http_code}" -X DELETE \
46+
local response
47+
response=$(curl --fail-with-body -s -w "\n%{http_code}" -X DELETE \
4948
-H "$AUTH_HEADER" \
5049
-H "$CONTENT_TYPE_HEADER" \
5150
"$API_URL_BASE/$previewName")
5251
local exit_code=$?
52+
local http_code=$(echo "$response" | tail -n1)
53+
local body=$(echo "$response" | sed '$d')
54+
5355
if [ $exit_code -ne 0 ]; then
54-
echo "Error deleting instance, curl failed with exit code $exit_code. API response: $http_code" >&2
55-
return 1
56+
# A 404 means it's already gone, which is a success case for shutdown.
57+
if [ "$http_code" -ne 404 ]; then
58+
echo "Error deleting instance, curl failed with exit code $exit_code. API response: $body" >&2
59+
return 1
60+
fi
5661
fi
5762
if [ "$http_code" -eq 200 ]; then
5863
echo "Instance '$previewName' deleted successfully."
5964
elif [ "$http_code" -eq 404 ]; then
6065
echo "Instance '$previewName' was already deleted (not found)."
6166
fi
6267
}
68+
6369
retry shutdown_instance
6470
6571
- name: Update status comment

ephemeral/startup/action.yml

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ runs:
6464
API_URL_BASE="https://api.localstack.cloud/v1/compute/instances"
6565
6666
source ${{ github.action_path }}/../retry-function.sh
67+
6768
fetch_instances() {
6869
local list_response
6970
list_response=$(curl --fail-with-body -s -X GET \
7071
-H "$AUTH_HEADER" \
7172
-H "$CONTENT_TYPE_HEADER" \
7273
"$API_URL_BASE")
73-
if [ $? -ne 0 ]; then
74-
echo "API returned an error: $list_response"
75-
return 1
76-
fi
74+
if [ $? -ne 0 ]; then echo "curl command failed while fetching instances. Response: $list_response" >&2; return 1; fi
75+
if ! check_for_api_error "$list_response" "fetch instances"; then return 1; fi
7776
echo "$list_response"
7877
}
7978
@@ -90,16 +89,18 @@ runs:
9089
9190
delete_instance() {
9291
# We expect a 200 on success or 404 if it's already gone. Other codes are errors.
93-
local http_code
94-
http_code=$(curl --fail-with-body -s -o /dev/null -w "%{http_code}" -X DELETE \
92+
local response
93+
response=$(curl --fail-with-body -s -w "\n%{http_code}" -X DELETE \
9594
-H "$AUTH_HEADER" \
9695
-H "$CONTENT_TYPE_HEADER" \
9796
"$API_URL_BASE/$previewName")
9897
local exit_code=$?
99-
if [ $exit_code -ne 0 ]; then
100-
echo "Error deleting instance, curl failed with exit code $exit_code. API response: $http_code" >&2
101-
return 1
102-
fi
98+
local http_code=$(echo "$response" | tail -n1)
99+
local body=$(echo "$response" | sed '$d')
100+
101+
if [ $exit_code -ne 0 ]; then echo "curl command failed while deleting instance. Response: $body" >&2; return 1; fi
102+
if ! check_for_api_error "$body" "delete instance"; then return 1; fi
103+
103104
if [ "$http_code" -eq 200 ]; then
104105
echo "Instance '$previewName' deleted successfully."
105106
fi
@@ -119,9 +120,10 @@ runs:
119120
-H "$AUTH_HEADER" \
120121
-H "$CONTENT_TYPE_HEADER" \
121122
"$API_URL_BASE")
122-
if [ $? -ne 0 ] || ! echo "$response" | jq -e 'has("endpoint_url") and (.endpoint_url | test(".+"))' > /dev/null; then
123-
echo "Unable to create preview environment. API response: $response"
124-
return 1
123+
if [ $? -ne 0 ]; then echo "curl command failed while creating instance. Response: $response" >&2; return 1; fi
124+
if ! check_for_api_error "$response" "create instance"; then return 1; fi
125+
if ! echo "$response" | jq -e 'has("endpoint_url") and (.endpoint_url | test(".+"))' > /dev/null; then
126+
echo "Invalid response from instance creation API: $response" >&2; return 1;
125127
fi
126128
echo "$response"
127129
}
@@ -169,22 +171,12 @@ runs:
169171
-H "$AUTH_HEADER" \
170172
-H "$CONTENT_TYPE_HEADER" \
171173
"$API_URL_BASE/$previewName/logs")
172-
if [ $? -ne 0 ]; then
173-
echo "API returned an error while fetching logs: $log_response"
174-
return 1
175-
fi
176-
if [ -z "$log_response" ]; then
177-
echo "API returned an empty response when fetching logs."
178-
return 1
179-
fi
174+
if [ $? -ne 0 ]; then echo "curl command failed while fetching logs. Response: $log_response" >&2; return 1; fi
175+
if ! check_for_api_error "$log_response" "fetch logs"; then return 1; fi
180176
181-
# Now, check if jq extracts any content
182-
local extracted_content
183-
extracted_content=$(echo "$log_response" | jq -r '.[].content')
184-
if [ -z "$extracted_content" ]; then
185-
echo "No content found in logs or logs are empty after extraction."
186-
echo "Raw API response for logs: $log_response" # This will be printed on retry attempts
187-
return 1
177+
# A valid log response must be a JSON array.
178+
if ! echo "$log_response" | jq -e 'if type == "array" then true else false end' > /dev/null; then
179+
echo "Invalid response from logs API (expected a JSON array): $log_response" >&2; return 1;
188180
fi
189181
190182
echo "$log_response"

0 commit comments

Comments
 (0)