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 authored Aug 29, 2023
1 parent e2b5aaa commit 6f187ca
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
47 changes: 41 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,19 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
continue;
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
if (currentResult == null)
{
// 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));

continue;
}

if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
_cmdletPassedIn.WriteError(new ErrorRecord(
Expand Down Expand Up @@ -812,7 +824,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 +841,19 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
continue;
}

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
if (currentResult == null)
{
// 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));

continue;
}

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

PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
if (currentResult == null)
{
// 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;
}

if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Describe 'Test Find-PSResource for searching and looping through repositories' -

It "find multiple resources from all repositories where it exists where package Name contains wildcard (without -Repository specified)" {
$res = Find-PSResource -Name "test_module*" -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -HaveCount 9
$res | Should -HaveCount 10
$err | Should -HaveCount 0

$pkgFoundinLocalRepo = $false
Expand Down
9 changes: 8 additions & 1 deletion test/FindPSResourceTests/FindPSResourceV2Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
BeforeAll{
$PSGalleryName = Get-PSGalleryName
$testModuleName = "test_module"
$testModuleNameWithUnlistedVersion = "test_module10"
$testScriptName = "test_script"
$commandName = "Get-TargetResource"
$dscResourceName = "SystemLocale"
Expand Down Expand Up @@ -397,6 +398,13 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
$res.Version | Should -Be "2.1.0"
$err.Count | Should -Be 0
}

It "should not find and write error when finding package version that is unlisted" {
$res = Find-PSResource -Name $testModuleNameWithUnlistedVersion -Version "1.0.0.0" -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
$res | Should -HaveCount 0
$err | Should -HaveCount 1
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
}
}

Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidationOnly' {
Expand Down Expand Up @@ -426,7 +434,6 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidat
{
if ($foundPkgs.Contains($item.Name))
{
write-host "this pkg already found"
$duplicatePkgsFound = $true
break
}
Expand Down

0 comments on commit 6f187ca

Please sign in to comment.