Skip to content

Add custom exception messages for PSGallery - #2037

Closed
shammu1 wants to merge 1 commit into
masterfrom
shammu/aksexceptions
Closed

shammu1 wants to merge 1 commit into
masterfrom
shammu/aksexceptions

Conversation

@shammu1

@shammu1 shammu1 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

PR Summary

This pull request enhances error handling and diagnostics when publishing packages to the PowerShell Gallery (PSGallery), making it easier to identify and troubleshoot authentication and permission issues.

PR Context

This PR is needed for PSResourceGET to provide the users the exceptions messages with exact reason phrase for PSGallery.

PR Checklist

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The 401 diagnostic and URI reuse need correction, and regression tests are missing for the new messages.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Improves PSGallery publishing diagnostics for authentication, authorization, and duplicate-package failures.

Changes:

  • Exposes the canonical PSGallery URI.
  • Adds custom 401, 403, and 409 error handling.
  • Extracts package identity for conflict diagnostics.
File summaries
File Description
src/code/RepositorySettings.cs Exposes the PSGallery URI constant.
src/code/PublishHelper.cs Adds package identity extraction and PSGallery-specific errors.
Review details

Suppressed comments (9)

src/code/PublishHelper.cs:787

  • The custom PSGallery 403 text has duplicated punctuation: the reason already ends with a period inside the parentheses, and the string adds another period after ). Since this PR is intended to expose the exact reason phrase, remove the trailing period outside the closing parenthesis.
                            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.)."),

src/code/PublishHelper.cs:811

  • The 409 message has the same duplicated punctuation (modified.).), so the package-conflict reason is not emitted as an exact sentence. Remove the extra period after the closing parenthesis.
                                $"(A package with id '{packageName}' and version '{packageVersion}' already exists and cannot be modified.).",

src/code/PublishHelper.cs:750

  • Please add a CI test for the PSGallery Unauthorized path that asserts the exception text, not only the error ID. No existing test exercises this new branch, so the intended credential/API-key diagnostic could regress while the current status-code assertion still passes.
                    else if (isPSGallery && e.Message.Contains("Unauthorized"))
                    {
                        // For AKS Exception
                        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)."),
                            "401Error",

src/code/PublishHelper.cs:787

  • The existing incorrect-API-key PSGallery test checks only FullyQualifiedErrorId, so it does not verify this newly introduced detailed 403 message. Please assert the full exception text here to protect the exact diagnostic promised by the PR.
                    else if (isPSGallery && e.Message.Contains("Forbidden"))
                    {
                        // For AKS Exception
                        error = new ErrorRecord(
                            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.)."),

src/code/PublishHelper.cs:807

  • Please add coverage for publishing the same package ID/version twice and assert that the 409 message contains the package identity. This new path depends on PackageArchiveReader and is otherwise untested, so both the duplicate detection diagnostic and identity extraction can regress unnoticed.
                    if (isPSGallery && e.Message.Contains("Conflict"))
                    {
                        // For AKS Exception
                        string packageName = _pkgName ?? "unknown";
                        string packageVersion = _pkgVersion?.ToNormalizedString() ?? "unknown";

src/code/PublishHelper.cs:748

  • This comment says the branch handles an AKS exception, but the condition is explicitly for PSGallery. Please correct it so the error-handling rationale is not misleading to future maintainers.
                        // For AKS Exception

src/code/PublishHelper.cs:785

  • This comment says the branch handles an AKS exception, but the condition is explicitly for PSGallery. Please correct it so the error-handling rationale is not misleading to future maintainers.
                        // For AKS Exception

src/code/PublishHelper.cs:805

  • This comment says the branch handles an AKS exception, but the condition is explicitly for PSGallery. Please correct it so the error-handling rationale is not misleading to future maintainers.
                        // For AKS Exception

src/code/RepositorySettings.cs:28

  • This exposes a shared constant, but RegisterPSResourceRepository.cs:34 still declares the same PSGallery URI separately. That leaves registration and the new PSGallery error classification with two sources of truth that can silently diverge when the endpoint changes; update the repository registration code to use this constant as well.
        internal const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/code/PublishHelper.cs
else if (isPSGallery && e.Message.Contains("Unauthorized"))
{
// For AKS Exception
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)."),
Comment thread src/code/PublishHelper.cs
Comment on lines +803 to +807
if (isPSGallery && e.Message.Contains("Conflict"))
{
// For AKS Exception
string packageName = _pkgName ?? "unknown";
string packageVersion = _pkgVersion?.ToNormalizedString() ?? "unknown";
@shammu1

shammu1 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@shammu1 shammu1 closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants