Skip to content

Commit 6f187ca

Browse files
authored
Add error handling for when a package version to be found in unlisted (#1384)
1 parent e2b5aaa commit 6f187ca

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/code/FindHelper.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
699699
else
700700
{
701701
responses = currentServer.FindNameGlobbingWithTag(pkgName, _tag, _prerelease, _type, out errRecord);
702-
tagsAsString = String.Join(", ", _tag);
702+
tagsAsString = $" and tags '{String.Join(", ", _tag)}'";
703703
}
704704

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

756756
if (errRecord != null)
@@ -767,7 +767,19 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
767767
continue;
768768
}
769769

770-
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
770+
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
771+
if (currentResult == null)
772+
{
773+
// This scenario may occur when the package version requested is unlisted.
774+
_cmdletPassedIn.WriteError(new ErrorRecord(
775+
new ResourceNotFoundException($"Package with name '{pkgName}'{tagsAsString} could not be found in repository '{repository.Name}'"),
776+
"PackageNotFound",
777+
ErrorCategory.ObjectNotFound,
778+
this));
779+
780+
continue;
781+
}
782+
771783
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
772784
{
773785
_cmdletPassedIn.WriteError(new ErrorRecord(
@@ -812,7 +824,7 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
812824
else
813825
{
814826
responses = currentServer.FindVersionWithTag(pkgName, _nugetVersion.ToNormalizedString(), _tag, _type, out errRecord);
815-
tagsAsString = String.Join(", ", _tag);
827+
tagsAsString = $" and tags '{String.Join(", ", _tag)}'";
816828
}
817829

818830
if (errRecord != null)
@@ -829,7 +841,19 @@ private IEnumerable<PSResourceInfo> SearchByNames(ServerApiCall currentServer, R
829841
continue;
830842
}
831843

832-
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
844+
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
845+
if (currentResult == null)
846+
{
847+
// This scenario may occur when the package version requested is unlisted.
848+
_cmdletPassedIn.WriteError(new ErrorRecord(
849+
new ResourceNotFoundException($"Package with name '{pkgName}', version '{_nugetVersion.ToNormalizedString()}'{tagsAsString} could not be found in repository '{repository.Name}'"),
850+
"PackageNotFound",
851+
ErrorCategory.ObjectNotFound,
852+
this));
853+
854+
continue;
855+
}
856+
833857
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
834858
{
835859
_cmdletPassedIn.WriteError(new ErrorRecord(
@@ -1041,7 +1065,18 @@ internal IEnumerable<PSResourceInfo> FindDependencyPackages(
10411065
continue;
10421066
}
10431067

1044-
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).First();
1068+
PSResourceResult currentResult = currentResponseUtil.ConvertToPSResourceResult(responses).FirstOrDefault();
1069+
if (currentResult == null)
1070+
{
1071+
// This scenario may occur when the package version requested is unlisted.
1072+
_cmdletPassedIn.WriteError(new ErrorRecord(
1073+
new ResourceNotFoundException($"Dependency package with name '{dep.Name}' could not be found in repository '{repository.Name}'"),
1074+
"DependencyPackageNotFound",
1075+
ErrorCategory.ObjectNotFound,
1076+
this));
1077+
yield return null;
1078+
continue;
1079+
}
10451080

10461081
if (currentResult.exception != null && !currentResult.exception.Message.Equals(string.Empty))
10471082
{

test/FindPSResourceTests/FindPSResourceRepositorySearching.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Describe 'Test Find-PSResource for searching and looping through repositories' -
128128

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

134134
$pkgFoundinLocalRepo = $false

test/FindPSResourceTests/FindPSResourceV2Server.Tests.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
1212
BeforeAll{
1313
$PSGalleryName = Get-PSGalleryName
1414
$testModuleName = "test_module"
15+
$testModuleNameWithUnlistedVersion = "test_module10"
1516
$testScriptName = "test_script"
1617
$commandName = "Get-TargetResource"
1718
$dscResourceName = "SystemLocale"
@@ -397,6 +398,13 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'CI' {
397398
$res.Version | Should -Be "2.1.0"
398399
$err.Count | Should -Be 0
399400
}
401+
402+
It "should not find and write error when finding package version that is unlisted" {
403+
$res = Find-PSResource -Name $testModuleNameWithUnlistedVersion -Version "1.0.0.0" -Repository $PSGalleryName -ErrorVariable err -ErrorAction SilentlyContinue
404+
$res | Should -HaveCount 0
405+
$err | Should -HaveCount 1
406+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
407+
}
400408
}
401409

402410
Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidationOnly' {
@@ -426,7 +434,6 @@ Describe 'Test HTTP Find-PSResource for V2 Server Protocol' -tags 'ManualValidat
426434
{
427435
if ($foundPkgs.Contains($item.Name))
428436
{
429-
write-host "this pkg already found"
430437
$duplicatePkgsFound = $true
431438
break
432439
}

0 commit comments

Comments
 (0)