Skip to content

Commit 7da6e3b

Browse files
alericksonCopilot
andcommitted
Fix error handling for GetCountFromResponse method
Co-authored-by: Copilot <copilot@github.com>
1 parent 4e3c8f2 commit 7da6e3b

2 files changed

Lines changed: 53 additions & 58 deletions

File tree

src/code/V2ServerAPICalls.cs

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public override FindResults FindAll(bool includePrerelease, ResourceType type, o
110110
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
111111
}
112112

113-
int initialScriptCount = GetCountFromResponse(initialScriptResponse, out errRecord);
114-
if (errRecord != null)
113+
int initialScriptCount = GetCountFromResponse(initialScriptResponse, out string errStr);
114+
if (errStr != null)
115115
{
116116
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
117117
}
@@ -145,8 +145,8 @@ public override FindResults FindAll(bool includePrerelease, ResourceType type, o
145145
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
146146
}
147147

148-
int initialModuleCount = GetCountFromResponse(initialModuleResponse, out errRecord);
149-
if (errRecord != null)
148+
int initialModuleCount = GetCountFromResponse(initialModuleResponse, out string errStr);
149+
if (errStr != null)
150150
{
151151
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
152152
}
@@ -197,8 +197,8 @@ public override FindResults FindTags(string[] tags, bool includePrerelease, Reso
197197
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
198198
}
199199

200-
int initialScriptCount = GetCountFromResponse(initialScriptResponse, out errRecord);
201-
if (errRecord != null)
200+
int initialScriptCount = GetCountFromResponse(initialScriptResponse, out string errStr);
201+
if (errStr != null)
202202
{
203203
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
204204
}
@@ -233,8 +233,8 @@ public override FindResults FindTags(string[] tags, bool includePrerelease, Reso
233233
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
234234
}
235235

236-
int initialModuleCount = GetCountFromResponse(initialModuleResponse, out errRecord);
237-
if (errRecord != null)
236+
int initialModuleCount = GetCountFromResponse(initialModuleResponse, out string errStr);
237+
if (errStr != null)
238238
{
239239
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
240240
}
@@ -287,8 +287,8 @@ public override FindResults FindCommandOrDscResource(string[] tags, bool include
287287
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
288288
}
289289

290-
int initialCount = GetCountFromResponse(initialResponse, out errRecord);
291-
if (errRecord != null)
290+
int initialCount = GetCountFromResponse(initialResponse, out string errStr);
291+
if (errStr != null)
292292
{
293293
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
294294
}
@@ -379,8 +379,8 @@ public override FindResults FindName(string packageName, bool includePrerelease,
379379
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
380380
}
381381

382-
int count = GetCountFromResponse(response, out errRecord);
383-
if (errRecord != null)
382+
int count = GetCountFromResponse(response, out string errStr);
383+
if (errStr != null)
384384
{
385385
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
386386
}
@@ -477,10 +477,14 @@ public override async Task<FindResults> FindNameAsync(string packageName, bool i
477477
}
478478
}
479479

480-
int count = GetCountFromResponse(response, out ErrorRecord errRecord);
481-
if (errRecord != null)
480+
int count = GetCountFromResponse(response, out string errStr);
481+
if (errStr != null)
482482
{
483-
Utils.EnqueueIfNotNull(errorMsgs, errRecord);
483+
errorMsgs.Enqueue(new ErrorRecord(
484+
new ResourceNotFoundException($"Package with name '{packageName}' could not be found in repository '{Repository.Name}': {errStr}"),
485+
"PackageNotFound",
486+
ErrorCategory.ObjectNotFound,
487+
this));
484488
}
485489
else if (count == 0)
486490
{
@@ -539,8 +543,8 @@ public override FindResults FindNameWithTag(string packageName, string[] tags, b
539543
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
540544
}
541545

542-
int count = GetCountFromResponse(response, out errRecord);
543-
if (errRecord != null)
546+
int count = GetCountFromResponse(response, out string errStr);
547+
if (errStr != null)
544548
{
545549
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
546550
}
@@ -581,8 +585,8 @@ public override FindResults FindNameGlobbing(string packageName, bool includePre
581585
responses.Add(initialResponse);
582586

583587
// check count (regex) 425 ==> count/100 ~~> 4 calls
584-
int initialCount = GetCountFromResponse(initialResponse, out errRecord); // count = 4
585-
if (errRecord != null)
588+
int initialCount = GetCountFromResponse(initialResponse, out string errStr); // count = 4
589+
if (errStr != null)
586590
{
587591
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
588592
}
@@ -634,8 +638,8 @@ public override FindResults FindNameGlobbingWithTag(string packageName, string[]
634638
responses.Add(initialResponse);
635639

636640
// check count (regex) 425 ==> count/100 ~~> 4 calls
637-
int initialCount = GetCountFromResponse(initialResponse, out errRecord); // count = 4
638-
if (errRecord != null)
641+
int initialCount = GetCountFromResponse(initialResponse, out string errStr); // count = 4
642+
if (errStr != null)
639643
{
640644
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
641645
}
@@ -686,8 +690,8 @@ public override FindResults FindVersionGlobbing(string packageName, VersionRange
686690
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
687691
}
688692

689-
int initialCount = GetCountFromResponse(initialResponse, out errRecord);
690-
if (errRecord != null)
693+
int initialCount = GetCountFromResponse(initialResponse, out string errStr);
694+
if (errStr != null)
691695
{
692696
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
693697
}
@@ -782,10 +786,10 @@ public override FindResults FindVersion(string packageName, string version, Reso
782786
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
783787
}
784788

785-
int count = GetCountFromResponse(response, out errRecord);
789+
int count = GetCountFromResponse(response, out string errStr);
786790
_cmdletPassedIn.WriteDebug($"Count from response is '{count}'");
787791

788-
if (errRecord != null)
792+
if (errStr != null)
789793
{
790794
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
791795
}
@@ -871,13 +875,13 @@ public override async Task<FindResults> FindVersionAsync(string packageName, str
871875
}
872876
}
873877

874-
int count = GetCountFromResponse(response, out ErrorRecord errRecord);
875-
if (errRecord != null)
878+
int count = GetCountFromResponse(response, out string errStr);
879+
if (errStr != null)
876880
{
877881
errorMsgs.Enqueue(new ErrorRecord(
878-
new Exception($"Error retrieving count from response for package '{packageName}' from repository '{Repository.Name}'.", errRecord.Exception),
879-
"GetCountFromResponseFailure",
880-
ErrorCategory.InvalidData,
882+
new ResourceNotFoundException($"Package with name '{packageName}', version '{version}' could not be found in repository '{Repository.Name}': {errStr}"),
883+
"PackageNotFound",
884+
ErrorCategory.ObjectNotFound,
881885
this));
882886
}
883887

@@ -933,10 +937,10 @@ public override FindResults FindVersionWithTag(string packageName, string versio
933937
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
934938
}
935939

936-
int count = GetCountFromResponse(response, out errRecord);
940+
int count = GetCountFromResponse(response, out string errStr);
937941
_cmdletPassedIn.WriteDebug($"Count from response is '{count}'");
938942

939-
if (errRecord != null)
943+
if (errStr != null)
940944
{
941945
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
942946
}
@@ -1714,10 +1718,14 @@ public override async Task<FindResults> FindVersionGlobbingAsync(string packageN
17141718

17151719
var initialResponse = await FindVersionGlobbingAsync(packageName, versionRange, includePrerelease, type, skip, getOnlyLatest, errorMsgs, warningMsgs, debugMsgs, verboseMsgs);
17161720

1717-
int initialCount = GetCountFromResponse(initialResponse, out ErrorRecord errRecord);
1718-
if (errRecord != null)
1721+
int initialCount = GetCountFromResponse(initialResponse, out string errStr);
1722+
if (errStr != null)
17191723
{
1720-
Utils.EnqueueIfNotNull(errorMsgs, errRecord);
1724+
errorMsgs.Enqueue(new ErrorRecord(
1725+
new ResourceNotFoundException($"Package with name '{packageName}' and version range '{versionRange.ToString()}' could not be found in repository '{Repository.Name}': {errStr}"),
1726+
"PackageNotFound",
1727+
ErrorCategory.ObjectNotFound,
1728+
this));
17211729
return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v2FindResponseType);
17221730
}
17231731

@@ -1738,7 +1746,7 @@ public override async Task<FindResults> FindVersionGlobbingAsync(string packageN
17381746
// skip 100
17391747
skip += 100;
17401748
// TODO: this should be an async method
1741-
var tmpResponse = FindVersionGlobbing(packageName, versionRange, includePrerelease, type, skip, getOnlyLatest, out errRecord);
1749+
var tmpResponse = FindVersionGlobbing(packageName, versionRange, includePrerelease, type, skip, getOnlyLatest, out ErrorRecord errRecord);
17421750
if (errRecord != null)
17431751
{
17441752
Utils.EnqueueIfNotNull(errorMsgs, errRecord);
@@ -1959,9 +1967,9 @@ private string GetTypeFilterForRequest(ResourceType type) {
19591967
/// Helper method that makes gets 'count' property from http response string.
19601968
/// The count property is used to determine the number of total results found (for pagination).
19611969
/// </summary>
1962-
public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
1970+
public int GetCountFromResponse(string httpResponse, out string errStr)
19631971
{
1964-
errRecord = null;
1972+
errStr = null;
19651973
int count = 0;
19661974

19671975
//Create the XmlDocument.
@@ -1980,11 +1988,7 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
19801988
if (node == null || String.IsNullOrWhiteSpace(node.InnerText))
19811989
{
19821990
countSearchSucceeded = false;
1983-
errRecord = new ErrorRecord(
1984-
new PSArgumentException("Count property from server response was empty, invalid or not present."),
1985-
"GetCountFromResponseFailure",
1986-
ErrorCategory.InvalidData,
1987-
this);
1991+
errStr = "Count property from server response was empty, invalid or not present.";
19881992
}
19891993
else
19901994
{
@@ -1999,25 +2003,16 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
19992003
if (elemList.Count > 0)
20002004
{
20012005
count = elemList.Count;
2002-
errRecord = null;
20032006
}
20042007
else
20052008
{
2006-
errRecord = new ErrorRecord(
2007-
new PSArgumentException("Property 'count' and 'd:Id' could not be found in response. This may indicate that the package could not be found"),
2008-
"GetCountFromResponseFailure",
2009-
ErrorCategory.InvalidData,
2010-
this);
2009+
errStr = "Property 'count' and 'd:Id' could not be found in response. This may indicate that the package could not be found";
20112010
}
20122011
}
20132012
}
20142013
catch (XmlException e)
20152014
{
2016-
errRecord = new ErrorRecord(
2017-
exception: e,
2018-
"GetCountFromResponse",
2019-
ErrorCategory.InvalidData,
2020-
this);
2015+
errStr = $"XML Exception occurred while parsing server response: {e.Message}";
20212016
}
20222017

20232018
return count;

test/FindPSResourceTests/FindPSResourceADOV2Server.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
190190
$res = Find-PSResource -Name $testModuleName -Tag $requiredTag -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
191191
$res | Should -BeNullOrEmpty
192192
$err.Count | Should -BeGreaterThan 0
193-
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetCountFromResponseFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
193+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
194194
}
195195

196196
It "Find resource that satisfies given Name and Tag property (multiple tags)" {
@@ -208,7 +208,7 @@ Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
208208
$res = Find-PSResource -Name $testModuleName -Tag $requiredTags -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
209209
$res | Should -BeNullOrEmpty
210210
$err.Count | Should -BeGreaterThan 0
211-
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetCountFromResponseFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
211+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
212212
}
213213

214214
It "Find all resources that satisfy Name pattern and have specified Tag (single tag)" {
@@ -244,7 +244,7 @@ Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
244244
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTag -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
245245
$res | Should -BeNullOrEmpty
246246
$err.Count | Should -BeGreaterThan 0
247-
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetCountFromResponseFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
247+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
248248
}
249249

250250
It "Find resource that satisfies given Name, Version and Tag property (multiple tags)" {
@@ -264,6 +264,6 @@ Describe 'Test HTTP Find-PSResource for ADO V2 Server Protocol' -tags 'CI' {
264264
$res = Find-PSResource -Name $testModuleName -Version "5.0.0" -Tag $requiredTags -Repository $ADOV2RepoName -ErrorVariable err -ErrorAction SilentlyContinue
265265
$res | Should -BeNullOrEmpty
266266
$err.Count | Should -BeGreaterThan 0
267-
$err[0].FullyQualifiedErrorId | Should -BeExactly "GetCountFromResponseFailure,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
267+
$err[0].FullyQualifiedErrorId | Should -BeExactly "PackageNotFound,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
268268
}
269269
}

0 commit comments

Comments
 (0)