Skip to content

Commit

Permalink
add error handling for when a package version to be found in unlisted
Browse files Browse the repository at this point in the history
  • Loading branch information
anamnavi committed Aug 28, 2023
1 parent e2b5aaa commit 0ed334f
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
else
{
responses = currentServer.FindNameGlobbingWithTag(pkgName, _tag, _prerelease, _type, out errRecord);
tagsAsString = String.Join(", ", _tag);
tagsAsString = $" and tags '{String.Join(", ", _tag)}'";
}

if (errRecord != null)
Expand Down Expand Up @@ -750,7 +750,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
else
{
responses = currentServer.FindNameWithTag(pkgName, _tag, _prerelease, _type, out errRecord);
tagsAsString = String.Join(", ", _tag);
tagsAsString = $" and tags '{String.Join(", ", _tag)}'";
}

if (errRecord != null)
Expand All @@ -767,7 +767,20 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
continue;
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
IEnumerable<PSResourceResult> responseUtilResults = currentResponseUtil.ConvertToPSResourceResult(responses);
if (responseUtilResults.Count() == 0)
{
// This scenario may occur when the package version requested is unlisted.
_cmdletPassedIn.WriteError(new ErrorRecord(
new ResourceNotFoundException($"Package with name '{pkgName}'{tagsAsString} could not be found in repository '{repository.Name}'"),
"PackageNotFound",
ErrorCategory.ObjectNotFound,
this));
yield return null;
continue;
}

PSResourceResult currentResult = responseUtilResults.First();
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down Expand Up @@ -812,7 +825,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
else
{
responses = currentServer.FindVersionWithTag(pkgName, _nugetVersion.ToNormalizedString(), _tag, _type, out errRecord);
tagsAsString = String.Join(", ", _tag);
tagsAsString = $" and tags '{String.Join(", ", _tag)}'";
}

if (errRecord != null)
Expand All @@ -829,7 +842,20 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
continue;
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
IEnumerable<PSResourceResult> responseUtilResults = currentResponseUtil.ConvertToPSResourceResult(responses);
if (responseUtilResults.Count() == 0)
{
// This scenario may occur when the package version requested is unlisted.
_cmdletPassedIn.WriteError(new ErrorRecord(
new ResourceNotFoundException($"Package with name '{pkgName}, version '{_nugetVersion.ToNormalizedString()}'{tagsAsString} could not be found in repository '{repository.Name}'"),
"PackageNotFound",
ErrorCategory.ObjectNotFound,
this));
yield return null;
continue;
}

PSResourceResult currentResult = responseUtilResults.First();
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down Expand Up @@ -1041,8 +1067,20 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
continue;
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
IEnumerable<PSResourceResult> responseUtilResults = currentResponseUtil.ConvertToPSResourceResult(responses);
if (responseUtilResults.Count() == 0)
{
// This scenario may occur when the package version requested is unlisted.
_cmdletPassedIn.WriteError(new ErrorRecord(
new ResourceNotFoundException($"Dependency package with name '{dep.Name}' could not be found in repository '{repository.Name}'"),
"DependencyPackageNotFound",
ErrorCategory.ObjectNotFound,
this));
yield return null;
continue;
}

PSResourceResult currentResult = responseUtilResults.First();
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down

0 comments on commit 0ed334f

Please sign in to comment.