Skip to content

Commit 8a6738a

Browse files
committed
Add better error handling for 401s
1 parent bff47c2 commit 8a6738a

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/code/V2ServerAPICalls.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,23 @@ private string HttpRequestCall(string requestUrlV2, out ErrorRecord errRecord)
515515

516516
response = SendV2RequestAsync(request, _sessionClient).GetAwaiter().GetResult();
517517
}
518-
catch (HttpRequestException e)
518+
catch (V3ResourceNotFoundException e)
519519
{
520-
errRecord = new ErrorRecord(e, "HttpRequestFallFailure", ErrorCategory.ConnectionError, this);
520+
errRecord = new ErrorRecord(e, "ResourceNotFound", ErrorCategory.InvalidResult, this);
521521
}
522-
catch (ArgumentNullException e)
522+
catch (UnauthorizedException e)
523523
{
524-
errRecord = new ErrorRecord(e, "HttpRequestFallFailure", ErrorCategory.ConnectionError, this);
524+
errRecord = new ErrorRecord(e, "UnauthorizedRequest", ErrorCategory.InvalidResult, this);
525525
}
526-
catch (InvalidOperationException e)
526+
catch (HttpRequestException e)
527527
{
528-
errRecord = new ErrorRecord(e, "HttpRequestFallFailure", ErrorCategory.ConnectionError, this);
528+
errRecord = new ErrorRecord(e, "HttpRequestCallFailure", ErrorCategory.ConnectionError, this);
529529
}
530+
catch (Exception e)
531+
{
532+
errRecord = new ErrorRecord(e, "HttpRequestCallFailure", ErrorCategory.ConnectionError, this);
533+
}
534+
530535

531536
return response;
532537
}

src/code/V3ServerAPICalls.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,15 +1138,15 @@ private string HttpRequestCall(string requestUrlV3, out ErrorRecord errRecord)
11381138

11391139
response = SendV3RequestAsync(request, _sessionClient).GetAwaiter().GetResult();
11401140
}
1141-
catch (HttpRequestException e)
1141+
catch (V3ResourceNotFoundException e)
11421142
{
1143-
errRecord = new ErrorRecord(e, "HttpRequestCallFailure", ErrorCategory.InvalidResult, this);
1143+
errRecord = new ErrorRecord(e, "ResourceNotFound", ErrorCategory.InvalidResult, this);
11441144
}
1145-
catch (ArgumentNullException e)
1145+
catch (UnauthorizedException e)
11461146
{
1147-
errRecord = new ErrorRecord(e, "HttpRequestCallFailure", ErrorCategory.InvalidResult, this);
1147+
errRecord = new ErrorRecord(e, "UnauthorizedRequest", ErrorCategory.InvalidResult, this);
11481148
}
1149-
catch (InvalidOperationException e)
1149+
catch (HttpRequestException e)
11501150
{
11511151
errRecord = new ErrorRecord(e, "HttpRequestCallFailure", ErrorCategory.InvalidResult, this);
11521152
}

test/FindPSResourceTests/FindPSResourceV3Server.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,13 @@ Describe 'Test HTTP Find-PSResource for V3 Server Protocol' -tags 'CI' {
285285
$res | Should -Not -BeNullOrEmpty
286286
$res.Count | Should -BeGreaterThan 300
287287
}
288+
289+
It "should throw unauthorized exception if private repository with no credentials" {
290+
Register-PSResourceRepository -Name "PrivateGallery" -Uri "https://gitlab.com/api/v4/projects/47456554/packages/nuget/index.json"
291+
$res = Find-PSResource -Name $testModuleName -Repository "PrivateGallery" -ErrorVariable err -ErrorAction SilentlyContinue
292+
Unregister-PSResourceRepository -Name "PrivateGallery"
293+
$res | Should -BeNullOrEmpty
294+
$err.Count | Should -BeGreaterThan 0
295+
$err[0].FullyQualifiedErrorId | Should -BeExactly "UnauthorizedRequest,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource"
296+
}
288297
}

0 commit comments

Comments
 (0)