Skip to content

Commit 63a1093

Browse files
shammu1Shammu Meenakshi Sundaram
andauthored
Add custom exception messages for PSGallery (#2038)
Co-authored-by: Shammu Meenakshi Sundaram <smeenakshisu@microsoft.com>
1 parent 285384b commit 63a1093

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/code/PublishHelper.cs‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,12 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
681681

682682
try
683683
{
684+
using (var packageReader = new PackageArchiveReader(fullNupkgFile))
685+
{
686+
var packageIdentity = packageReader.GetIdentity();
687+
_pkgName = packageIdentity.Id;
688+
_pkgVersion = packageIdentity.Version;
689+
}
684690
PushRunner.Run(
685691
settings: Settings.LoadDefaultSettings(root: null, configFileName: null, machineWideSettings: null),
686692
sourceProvider: sourceProvider,
@@ -699,6 +705,8 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
699705
}
700706
catch (HttpRequestException e)
701707
{
708+
string normalizedRepoUri = repoUri.TrimEnd('/');
709+
bool isPSGallery = normalizedRepoUri.Equals(RepositorySettings.PSGalleryRepoUri, StringComparison.OrdinalIgnoreCase);
702710
_cmdletPassedIn.WriteVerbose(string.Format("Not able to publish resource to '{0}'", repoUri));
703711
// look in PS repo for how httpRequestExceptions are handled
704712

@@ -735,6 +743,14 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
735743
ErrorCategory.AuthenticationError,
736744
this);
737745
}
746+
else if (isPSGallery && e.Message.Contains("Unauthorized"))
747+
{
748+
// For AKS Exception
749+
error = new ErrorRecord(new ArgumentException($"Could not publish to repository '{repoName}'. The Credential provided was incorrect. Exception: Response status code does not indicate success: 401 (An API key must be provided)."),
750+
"401Error",
751+
ErrorCategory.AuthenticationError,
752+
this);
753+
}
738754
else
739755
{
740756
// For ADO repository feeds that are public feeds, when the credentials are incorrect.
@@ -764,6 +780,15 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
764780
ErrorCategory.PermissionDenied,
765781
this);
766782
}
783+
else if (isPSGallery && e.Message.Contains("Forbidden"))
784+
{
785+
// For AKS Exception
786+
error = new ErrorRecord(
787+
new ArgumentException($"Could not publish to repository '{repoName}'. Response status code does not indicate success: 403 (The specified API key is invalid, has expired, or does not have permission to access the specified package.)."),
788+
"403Error",
789+
ErrorCategory.PermissionDenied,
790+
this);
791+
}
767792
else
768793
{
769794
error = new ErrorRecord(
@@ -775,10 +800,26 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
775800
}
776801
else if (e.Message.Contains("409"))
777802
{
778-
error = new ErrorRecord(
803+
if (isPSGallery && e.Message.Contains("Conflict"))
804+
{
805+
// For AKS Exception
806+
string packageName = _pkgName ?? "unknown";
807+
string packageVersion = _pkgVersion?.ToNormalizedString() ?? "unknown";
808+
error = new ErrorRecord(
809+
new ArgumentException(
810+
$"Repository '{repoName}': Response status code does not indicate success: 409 " +
811+
$"(A package with id '{packageName}' and version '{packageVersion}' already exists and cannot be modified.).",
812+
e),
813+
"409Error",
814+
ErrorCategory.PermissionDenied, this);
815+
}
816+
else
817+
{
818+
error = new ErrorRecord(
779819
ex,
780820
"409Error",
781821
ErrorCategory.PermissionDenied, this);
822+
}
782823
}
783824
else
784825
{

‎src/code/RepositorySettings.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class RepositorySettings
2525
// File name for a user's repository store file is 'PSResourceRepository.xml'
2626
// The repository store file's location is currently only at '%LOCALAPPDATA%\PSResourceGet' for the user account.
2727
private const string PSGalleryRepoName = "PSGallery";
28-
private const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
28+
internal const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
2929
private const string MARRepoName = "MicrosoftArtifactRegistry";
3030
private const string MARRepoUri = "https://mcr.microsoft.com";
3131
private const int DefaultPriority = 50;

0 commit comments

Comments
 (0)