fix(setup-tenants): improve error handling to prevent silent failures

- Add robust error handling to check_existing function
- Fix create_social_links to handle API errors gracefully
- Fix create_pages to handle API errors and track skipped/failed counts
- Fix create_navigation with proper error handling
- Replace ((count++)) with $((count + 1)) for POSIX compatibility
- Add 2>/dev/null to jq calls to suppress error output
- Return 0 from functions on early exit to prevent set -e issues

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Martin Porwoll 2026-01-18 10:42:10 +00:00
parent 77f70876f4
commit 0976a691ec

View file

@ -283,8 +283,20 @@ api_get() {
# Check if documents exist for tenant
check_existing() {
local collection="$1"
local response
local count
count=$(api_get "$collection?where[tenant][equals]=$TENANT_ID&limit=1" | jq '.totalDocs // 0')
response=$(api_get "$collection?where[tenant][equals]=$TENANT_ID&limit=1")
debug "check_existing response: $response"
# Handle API errors or invalid JSON
if ! count=$(echo "$response" | jq -e '.totalDocs // 0' 2>/dev/null); then
warn "Failed to check existing $collection (API error or invalid response)"
debug "Response was: $response"
echo "0"
return 0
fi
echo "$count"
}
@ -328,32 +340,49 @@ create_social_links() {
local config_file="$SCRIPT_DIR/tenants/$TENANT/social-links.json"
if [[ ! -f "$config_file" ]]; then
warn "No social-links.json found for $TENANT"
return
return 0
fi
# Check existing
local existing
existing=$(check_existing "social-links")
existing=$(check_existing "social-links") || existing=0
if [[ "$existing" -gt 0 ]]; then
warn "Social Links already exist ($existing found), skipping..."
return
return 0
fi
local count=0
local failed=0
local link
local links
links=$(jq -c '.[]' "$config_file" 2>/dev/null) || {
warn "Failed to parse social-links.json"
return 0
}
while IFS= read -r link; do
[[ -z "$link" ]] && continue
local data
data=$(echo "$link" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}')
data=$(echo "$link" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}') || continue
local platform
platform=$(echo "$link" | jq -r '.platform')
platform=$(echo "$link" | jq -r '.platform') || platform="unknown"
if api_post "social-links" "$data" "$platform" > /dev/null; then
((count++))
debug "Creating social link: $platform"
if api_post "social-links" "$data" "$platform" > /dev/null 2>&1; then
count=$((count + 1))
else
failed=$((failed + 1))
warn "Failed to create social link: $platform"
fi
done < <(jq -c '.[]' "$config_file")
done <<< "$links"
if [[ "$failed" -gt 0 ]]; then
warn "Created $count Social Links ($failed failed)"
else
success "Created $count Social Links"
fi
}
#######################################
@ -365,45 +394,65 @@ create_pages() {
local config_file="$SCRIPT_DIR/tenants/$TENANT/pages.json"
if [[ ! -f "$config_file" ]]; then
warn "No pages.json found for $TENANT"
return
return 0
fi
local count=0
local skipped=0
local failed=0
local pages
pages=$(jq -c '.[]' "$config_file" 2>/dev/null) || {
warn "Failed to parse pages.json"
return 0
}
local page
while IFS= read -r page; do
local slug
slug=$(echo "$page" | jq -r '.slug')
local title
title=$(echo "$page" | jq -r '.title')
[[ -z "$page" ]] && continue
local slug title
slug=$(echo "$page" | jq -r '.slug' 2>/dev/null) || continue
title=$(echo "$page" | jq -r '.title' 2>/dev/null) || title="$slug"
# Check if page exists
local existing
existing=$(api_get "pages?where[tenant][equals]=$TENANT_ID&where[slug][equals]=$slug&limit=1" | jq '.totalDocs // 0')
local response existing
response=$(api_get "pages?where[tenant][equals]=$TENANT_ID&where[slug][equals]=$slug&limit=1") || response="{}"
existing=$(echo "$response" | jq '.totalDocs // 0' 2>/dev/null) || existing=0
if [[ "$existing" -gt 0 ]]; then
debug "Page '$slug' already exists, skipping..."
local id
id=$(api_get "pages?where[tenant][equals]=$TENANT_ID&where[slug][equals]=$slug&limit=1" | jq -r '.docs[0].id')
PAGE_ID_MAP["$slug"]="$id"
id=$(echo "$response" | jq -r '.docs[0].id' 2>/dev/null) || id=""
[[ -n "$id" && "$id" != "null" ]] && PAGE_ID_MAP["$slug"]="$id"
skipped=$((skipped + 1))
continue
fi
local data
data=$(echo "$page" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}')
data=$(echo "$page" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}') || continue
local response
if response=$(api_post "pages" "$data" "$title"); then
local post_response
if post_response=$(api_post "pages" "$data" "$title" 2>/dev/null); then
local id
id=$(echo "$response" | jq -r '.doc.id // .id // 0')
id=$(echo "$post_response" | jq -r '.doc.id // .id // 0' 2>/dev/null) || id="0"
if [[ "$id" != "0" && "$id" != "null" ]]; then
PAGE_ID_MAP["$slug"]="$id"
((count++))
count=$((count + 1))
debug "Created page: $title (ID: $id)"
else
failed=$((failed + 1))
fi
else
failed=$((failed + 1))
warn "Failed to create page: $title"
fi
done < <(jq -c '.[]' "$config_file")
done <<< "$pages"
success "Created $count Pages"
if [[ "$failed" -gt 0 ]]; then
warn "Created $count Pages ($skipped skipped, $failed failed)"
else
success "Created $count Pages ($skipped already existed)"
fi
# Show page ID mapping
if [[ "$VERBOSE" == "true" ]]; then
@ -423,33 +472,41 @@ create_navigation() {
local config_file="$SCRIPT_DIR/tenants/$TENANT/navigation.json"
if [[ ! -f "$config_file" ]]; then
warn "No navigation.json found for $TENANT"
return
return 0
fi
# Check existing
local existing
existing=$(check_existing "navigations")
existing=$(check_existing "navigations") || existing=0
if [[ "$existing" -gt 0 ]]; then
warn "Navigation already exists, skipping..."
return
return 0
fi
# Read navigation template and replace page slugs with IDs
local nav_data
nav_data=$(cat "$config_file")
nav_data=$(cat "$config_file") || {
warn "Failed to read navigation.json"
return 0
}
# Replace placeholders with actual page IDs
for slug in "${!PAGE_ID_MAP[@]}"; do
local id="${PAGE_ID_MAP[$slug]}"
nav_data=$(echo "$nav_data" | sed "s/\"PAGE_ID_${slug^^}\"/\"$id\"/g" | sed "s/\"PAGE_ID_$slug\"/$id/g")
nav_data=$(echo "$nav_data" | sed "s/\"PAGE_ID_${slug^^}\"/\"$id\"/g" | sed "s/\"PAGE_ID_$slug\"/$id/g") || true
done
# Add tenant ID
nav_data=$(echo "$nav_data" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}')
nav_data=$(echo "$nav_data" | jq --argjson tid "$TENANT_ID" '. + {tenant: $tid}' 2>/dev/null) || {
warn "Failed to add tenant ID to navigation data"
return 0
}
if api_post "navigations" "$nav_data" "Navigation" > /dev/null; then
if api_post "navigations" "$nav_data" "Navigation" > /dev/null 2>&1; then
success "Navigation created"
else
warn "Failed to create navigation"
fi
}