Skip to content

Commit c1ea89b

Browse files
committed
fix: improve error handling in test scripts
- Show HTTP error codes and response bodies instead of silent failures - Move token fetch outside polling loop in watch-status.sh - Distinguish 401/403 auth errors from 404 not-found in watch-status.sh - Document flat JSON limitation in write-firestore-doc.sh
1 parent 39409a5 commit c1ea89b

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

.skills/test-extension/scripts/delete-firestore-doc.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ ACCESS_TOKEN=$(gcloud auth application-default print-access-token 2>/dev/null ||
1414
BASE_URL="https://firestore.googleapis.com/v1/projects/${PROJECT_ID}/databases/(default)/documents"
1515

1616
echo "Deleting document: ${DOC_PATH}"
17-
curl -sf -X DELETE "${BASE_URL}/${DOC_PATH}" \
18-
-H "Authorization: Bearer ${ACCESS_TOKEN}" > /dev/null
17+
18+
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE "${BASE_URL}/${DOC_PATH}" \
19+
-H "Authorization: Bearer ${ACCESS_TOKEN}")
20+
21+
if [ "$HTTP_CODE" -ge 400 ] 2>/dev/null; then
22+
echo "ERROR: Firestore API returned HTTP ${HTTP_CODE}" >&2
23+
exit 1
24+
fi
1925

2026
echo "Document deleted."

.skills/test-extension/scripts/read-firestore-doc.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ SEGMENT_COUNT=$(echo "$DOC_PATH" | tr '/' '\n' | wc -l | tr -d ' ')
1919

2020
if [ $((SEGMENT_COUNT % 2)) -eq 1 ]; then
2121
echo "Listing documents in: ${DOC_PATH}"
22-
curl -sf "${BASE_URL}/${DOC_PATH}" \
23-
-H "Authorization: Bearer ${ACCESS_TOKEN}" | jq .
2422
else
2523
echo "Reading document: ${DOC_PATH}"
26-
curl -sf "${BASE_URL}/${DOC_PATH}" \
27-
-H "Authorization: Bearer ${ACCESS_TOKEN}" | jq .
2824
fi
25+
26+
RESPONSE=$(curl -s -w "\n%{http_code}" "${BASE_URL}/${DOC_PATH}" \
27+
-H "Authorization: Bearer ${ACCESS_TOKEN}")
28+
29+
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
30+
BODY=$(echo "$RESPONSE" | sed '$d')
31+
32+
if [ "$HTTP_CODE" -ge 400 ] 2>/dev/null; then
33+
echo "ERROR: Firestore API returned HTTP ${HTTP_CODE}" >&2
34+
echo "$BODY" | jq . 2>/dev/null || echo "$BODY" >&2
35+
exit 1
36+
fi
37+
38+
echo "$BODY" | jq .

.skills/test-extension/scripts/watch-status.sh

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,43 @@ TIMEOUT="${3:-60}"
1313
PROJECT_ID="${PROJECT_ID:?Set PROJECT_ID environment variable}"
1414

1515
BASE_URL="https://firestore.googleapis.com/v1/projects/${PROJECT_ID}/databases/(default)/documents"
16+
ACCESS_TOKEN=$(gcloud auth application-default print-access-token 2>/dev/null || gcloud auth print-access-token)
1617

1718
echo "Watching ${DOC_PATH} for status.state=${TARGET_STATE} (timeout: ${TIMEOUT}s)..."
1819

1920
elapsed=0
2021
while [ $elapsed -lt "$TIMEOUT" ]; do
21-
ACCESS_TOKEN=$(gcloud auth application-default print-access-token 2>/dev/null || gcloud auth print-access-token)
22+
RESPONSE=$(curl -s -w "\n%{http_code}" "${BASE_URL}/${DOC_PATH}" \
23+
-H "Authorization: Bearer ${ACCESS_TOKEN}")
2224

23-
RESPONSE=$(curl -sf "${BASE_URL}/${DOC_PATH}" \
24-
-H "Authorization: Bearer ${ACCESS_TOKEN}" 2>/dev/null || echo "{}")
25+
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
26+
BODY=$(echo "$RESPONSE" | sed '$d')
2527

26-
STATE=$(echo "$RESPONSE" | jq -r '.fields.status.mapValue.fields.state.stringValue // empty' 2>/dev/null)
28+
if [ "$HTTP_CODE" = "401" ] || [ "$HTTP_CODE" = "403" ]; then
29+
echo "ERROR: Authentication failed (HTTP ${HTTP_CODE}). Check gcloud auth." >&2
30+
echo "$BODY" | jq . 2>/dev/null || echo "$BODY" >&2
31+
exit 1
32+
fi
33+
34+
if [ "$HTTP_CODE" = "404" ]; then
35+
echo " [${elapsed}s] Document not found yet"
36+
sleep 2
37+
elapsed=$((elapsed + 2))
38+
continue
39+
fi
40+
41+
STATE=$(echo "$BODY" | jq -r '.fields.status.mapValue.fields.state.stringValue // empty' 2>/dev/null)
2742

2843
if [ "$STATE" = "$TARGET_STATE" ]; then
2944
echo "Status reached: ${TARGET_STATE} (after ${elapsed}s)"
30-
echo "$RESPONSE" | jq .
45+
echo "$BODY" | jq .
3146
exit 0
3247
fi
3348

3449
if [ "$STATE" = "ERRORED" ] && [ "$TARGET_STATE" != "ERRORED" ]; then
35-
ERROR_MSG=$(echo "$RESPONSE" | jq -r '.fields.status.mapValue.fields.error.stringValue // "unknown error"' 2>/dev/null)
50+
ERROR_MSG=$(echo "$BODY" | jq -r '.fields.status.mapValue.fields.error.stringValue // "unknown error"' 2>/dev/null)
3651
echo "ERROR: Extension errored after ${elapsed}s: ${ERROR_MSG}"
37-
echo "$RESPONSE" | jq .
52+
echo "$BODY" | jq .
3853
exit 1
3954
fi
4055

.skills/test-extension/scripts/write-firestore-doc.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ set -euo pipefail
66
#
77
# Writes a document to Firestore in the target project. JSON values are
88
# converted to Firestore field format automatically.
9+
# Supports flat JSON with string, number, boolean, and null values.
10+
# Nested objects and arrays are stringified — use the Firestore REST API
11+
# directly if you need complex nested structures.
912
# Requires: PROJECT_ID env var, gcloud auth (application-default credentials)
1013

1114
COLLECTION="${1:?Usage: write-firestore-doc.sh <collection> '<json>'}"
@@ -33,13 +36,22 @@ FIELDS=$(echo "$JSON_DATA" | jq '{
3336
}) | from_entries)
3437
}')
3538

36-
RESPONSE=$(curl -sf -X POST "${BASE_URL}/${COLLECTION}" \
39+
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/${COLLECTION}" \
3740
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
3841
-H "Content-Type: application/json" \
3942
-d "$FIELDS")
4043

41-
DOC_NAME=$(echo "$RESPONSE" | jq -r '.name')
44+
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
45+
BODY=$(echo "$RESPONSE" | sed '$d')
46+
47+
if [ "$HTTP_CODE" -ge 400 ] 2>/dev/null; then
48+
echo "ERROR: Firestore API returned HTTP ${HTTP_CODE}" >&2
49+
echo "$BODY" | jq . 2>/dev/null || echo "$BODY" >&2
50+
exit 1
51+
fi
52+
53+
DOC_NAME=$(echo "$BODY" | jq -r '.name')
4254
DOC_ID=$(echo "$DOC_NAME" | rev | cut -d'/' -f1 | rev)
4355

4456
echo "Document created: ${COLLECTION}/${DOC_ID}"
45-
echo "$RESPONSE" | jq .
57+
echo "$BODY" | jq .

0 commit comments

Comments
 (0)