diff --git a/lib/utils.bash b/lib/utils.bash index cc716cb..d44340a 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -46,9 +46,33 @@ sort_versions() { } fetch_all_assets() { - curl -s -H "Accept: application/vnd.github.v3+json" \ - https://api.github.com/repos/${GH_REPO}/releases | - jq -r '.[0].assets[] | "\(.name) \(.browser_download_url)"' + local response + response=$(curl -s -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/${GH_REPO}/releases") + + # Check if response is valid JSON + if ! echo "$response" | jq empty 2>/dev/null; then + fail "Failed to fetch releases from GitHub API. Invalid JSON response." + fi + + # Check if response is an array (releases endpoint returns array) + if ! echo "$response" | jq -e 'type == "array"' >/dev/null 2>&1; then + # Response is not an array, likely an error from GitHub API + local error_message + error_message=$(echo "$response" | jq -r '.message // "Unknown API error"' 2>/dev/null || echo "API returned unexpected format") + fail "GitHub API error: ${error_message}. Please check your network connection and GitHub API status." + fi + + # Check if there are any releases + local release_count + release_count=$(echo "$response" | jq 'length') + if [ "$release_count" -eq 0 ]; then + fail "No releases found in repository ${GH_REPO}." + fi + + # Extract assets from the first release safely with built-in type checking + # This provides additional protection against edge cases + echo "$response" | jq -r 'if type == "array" and length > 0 then .[0].assets[]? | "\(.name) \(.browser_download_url)" else empty end' } validate_platform() {