Skip to content

Commit

Permalink
issue dotnet#11: handle http status code 404 separately
Browse files Browse the repository at this point in the history
  • Loading branch information
AR-May committed Nov 24, 2020
1 parent c86e8c6 commit 0e67504
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
53 changes: 46 additions & 7 deletions src/dotnet-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,34 @@ function GetHTTPResponse([Uri] $Uri)
$Task = $HttpClient.GetAsync("${Uri}${FeedCredential}").ConfigureAwait("false");
$Response = $Task.GetAwaiter().GetResult();
if (($Response -eq $null) -or (-not ($Response.IsSuccessStatusCode))) {
# The feed credential is potentially sensitive info. Do not log FeedCredential to console output.
$ErrorMsg = "Failed to download $Uri."
# The feed credential is potentially sensitive info. Do not log FeedCredential to console output.
$ErrorMsg = ""
if ($Response -ne $null) {
$ErrorMsg += " $Response"
$ErrorMsg += "$Response"
}

throw $ErrorMsg
}

return $Response

}
catch {
$ErrorMsg = "Failed to download $Uri.`r`n"

# Pick up the exception message and inner exceptions' messages if they exist
$CurrentException = $PSItem.Exception
$ErrorMsg += $CurrentException.Message + "`r`n"
while ($CurrentException.InnerException) {
$CurrentException = $CurrentException.InnerException
$ErrorMsg += " " + $CurrentException.Message + "`r`n"
}

# Check if it is the issue with TLS.
if ($ErrorMsg -like "*SSL/TLS*") {
$ErrorMsg += "Ensure that TLS 1.2 or higher is enabled to use this script.`r`n"
}

throw "$ErrorMsg"
}
finally {
if ($HttpClient -ne $null) {
Expand Down Expand Up @@ -750,12 +768,21 @@ $ZipPath = [System.IO.Path]::combine([System.IO.Path]::GetTempPath(), [System.IO
Say-Verbose "Zip path: $ZipPath"

$DownloadFailed = $false
$DownloadFailedWithNon404Error = $false
$DownloadFailedMsg = ""
Say "Downloading link: $DownloadLink"
try {
DownloadFile -Source $DownloadLink -OutPath $ZipPath
}
catch {
Say "Cannot download: $DownloadLink"
if (($PSItem.Exception -like "*StatusCode: 404*")) {
Say "The primary path $DownloadLink is not available."
} else {
Say "Cannot download via the primary path $DownloadLink."
$DownloadFailedWithNon404Error = $true
$DownloadFailedMsg += $PSItem.Exception.Message + "`n"
}

SafeRemoveFile -Path $ZipPath

if ($LegacyDownloadLink) {
Expand All @@ -767,7 +794,14 @@ catch {
DownloadFile -Source $DownloadLink -OutPath $ZipPath
}
catch {
Say "Cannot download: $DownloadLink"
if (($PSItem.Exception -like "*StatusCode: 404*")) {
Say "The legacy path $DownloadLink is not available."
} else {
Say "Cannot download via the legacy path $DownloadLink."
$DownloadFailedWithNon404Error = $true
$DownloadFailedMsg += $PSItem.Exception.Message + "`n"
}

SafeRemoveFile -Path $ZipPath
$DownloadFailed = $true
}
Expand All @@ -778,7 +812,12 @@ catch {
}

if ($DownloadFailed) {
throw "Could not find/download: `"$assetName`" with version = $SpecificVersion`nRefer to: https://aka.ms/dotnet-os-lifecycle for information on .NET Core support"
if ($DownloadFailedWithNon404Error) {
Say-Error "$DownloadFailedMsg"
throw "Could not download `"$assetName`" with version = $SpecificVersion`nRefer to: https://aka.ms/dotnet-os-lifecycle for information on .NET Core support"
} else {
throw "Could not find `"$assetName`" with version = $SpecificVersion`nRefer to: https://aka.ms/dotnet-os-lifecycle for information on .NET Core support"
}
}

Say "Extracting zip from $DownloadLink"
Expand Down
33 changes: 29 additions & 4 deletions src/dotnet-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -848,10 +848,18 @@ install_dotnet() {
# the download function will fill variables $http_code and $download_error_msg in case of failure.
http_code=""; download_error_msg=""
download "$download_link" "$zip_path" 2>&1 || download_failed=true
primary_path_http_code="$http_code"; primary_path_download_error_msg="$download_error_msg"
# if the download fails, download the legacy_download_link
if [ "$download_failed" = true ]; then
say "Cannot download: $download_link"
case $primary_path_http_code in
404)
say "The primary path $download_link is not available."
;;
*)
say "Cannot download via the primary path $download_link. Returned HTTP status code: $primary_path_http_code."
;;
esac
rm -f "$zip_path" 2>&1 && say_verbose "Temporary zip file $zip_path was removed"
if [ "$valid_legacy_download_link" = true ]; then
download_failed=false
Expand All @@ -863,16 +871,33 @@ install_dotnet() {
# the download function will fill variables $http_code and $download_error_msg in case of failure.
http_code=""; download_error_msg=""
download "$download_link" "$zip_path" 2>&1 || download_failed=true
legacy_path_http_code="$http_code"; legacy_path_download_error_msg="$download_error_msg"
if [ "$download_failed" = true ]; then
say "Cannot download: $download_link"
case $legacy_path_http_code in
404)
say "The legacy path $download_link is not available."
;;
*)
say "Cannot download via the legacy path $download_link. Returned HTTP status code: $legacy_path_http_code."
;;
esac
rm -f "$zip_path" 2>&1 && say_verbose "Temporary zip file $zip_path was removed"
fi
fi
fi
if [ "$download_failed" = true ]; then
say_err "Could not find/download: \`$asset_name\` with version = $specific_version"
if [[ "$primary_path_http_code" = "404" && "$legacy_path_http_code" = "404" ]]; then
say_err "Could not find \`$asset_name\` with version = $specific_version"
else
say_err "Could not download: \`$asset_name\` with version = $specific_version:"
if [ "$primary_path_http_code" != "404" ]; then
say_err "$primary_path_download_error_msg"
fi
if [ "$legacy_path_http_code" != "404" ]; then
say_err "$legacy_path_download_error_msg"
fi
fi
say_err "Refer to: https://aka.ms/dotnet-os-lifecycle for information on .NET Core support"
return 1
fi
Expand Down

0 comments on commit 0e67504

Please sign in to comment.