Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle Error in async initialization of NuGet V3 API client #567

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions src/NuGetForUnity/Editor/PackageSource/NugetApiClientV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal sealed class NugetApiClientV3 : IDisposable
private readonly HttpClient httpClient = new HttpClient(
new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });

private bool initializationFailed;

// Example: https://api.nuget.org/v3-flatcontainer/
[SerializeField]
private string packageBaseAddress;
Expand Down Expand Up @@ -102,6 +104,12 @@ public async Task<List<INugetPackage>> SearchPackage(
bool includePreRelease = false,
CancellationToken cancellationToken = default)
{
if (initializationFailed)
{
Debug.LogError($"Initialization of api client for '{apiIndexJsonUrl}' failed so we can't search in it (see other error).");
return new List<INugetPackage>();
}

while (searchQueryServices == null)
{
// waiting for InitializeApiAddresses to complete
Expand Down Expand Up @@ -263,42 +271,50 @@ private string GetSessionStateKey()

private async void InitializeApiAddresses(NugetPackageSourceV3 packageSource)
{
var responseString = await GetStringFromServerAsync(packageSource, apiIndexJsonUrl.AbsoluteUri, CancellationToken.None)
.ConfigureAwait(false);
var resourceList =
JsonUtility.FromJson<IndexResponse>(responseString.Replace(@"""@id"":", @"""atId"":").Replace(@"""@type"":", @"""atType"":"));
var foundSearchQueryServices = new List<string>();
foreach (var resource in resourceList.resources)
try
{
switch (resource.atType)
var responseString = await GetStringFromServerAsync(packageSource, apiIndexJsonUrl.AbsoluteUri, CancellationToken.None)
.ConfigureAwait(false);
var resourceList =
JsonUtility.FromJson<IndexResponse>(responseString.Replace(@"""@id"":", @"""atId"":").Replace(@"""@type"":", @"""atType"":"));
var foundSearchQueryServices = new List<string>();
foreach (var resource in resourceList.resources)
{
case "SearchQueryService":
if (resource.comment.Contains("(primary)"))
{
foundSearchQueryServices.Insert(0, resource.atId.Trim('/'));
}
else
{
foundSearchQueryServices.Add(resource.atId.Trim('/'));
}
switch (resource.atType)
{
case "SearchQueryService":
if (resource.comment.Contains("(primary)"))
{
foundSearchQueryServices.Insert(0, resource.atId.Trim('/'));
}
else
{
foundSearchQueryServices.Add(resource.atId.Trim('/'));
}

break;
case "PackageBaseAddress/3.0.0":
packageBaseAddress = resource.atId.Trim('/') + '/';
break;
case "RegistrationsBaseUrl/3.6.0":
registrationsBaseUrl = resource.atId.Trim('/') + '/';
break;
}
}

break;
case "PackageBaseAddress/3.0.0":
packageBaseAddress = resource.atId.Trim('/') + '/';
break;
case "RegistrationsBaseUrl/3.6.0":
registrationsBaseUrl = resource.atId.Trim('/') + '/';
break;
if (string.IsNullOrEmpty(packageBaseAddress))
{
Debug.LogErrorFormat("The NuGet package source at '{0}' has no PackageBaseAddress resource defined.", apiIndexJsonUrl);
}
}

if (string.IsNullOrEmpty(packageBaseAddress))
searchQueryServices = foundSearchQueryServices;
SaveToSessionState();
}
catch (Exception exception)
{
Debug.LogErrorFormat("The NuGet package source at '{0}' has no PackageBaseAddress resource defined.", apiIndexJsonUrl);
Debug.LogErrorFormat("Failed to initialize the NuGet package source '{0}'. Error: {1}", apiIndexJsonUrl, exception);
initializationFailed = true;
}

searchQueryServices = foundSearchQueryServices;
SaveToSessionState();
}

private async Task<string> GetStringFromServerAsync(NugetPackageSourceV3 packageSource, string url, CancellationToken cancellationToken)
Expand Down
Loading