Skip to content

Add custom exception messages for PSGallery - #2034

Open
shammu1 wants to merge 1 commit into
PowerShell:masterfrom
shammu1:shammu/aksexceptions
Open

Add custom exception messages for PSGallery#2034
shammu1 wants to merge 1 commit into
PowerShell:masterfrom
shammu1:shammu/aksexceptions

Conversation

@shammu1

@shammu1 shammu1 commented Sep 11, 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.

@shammu1

shammu1 commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

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

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

Address the moderate diagnostics, test coverage, and URI normalization findings before approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

This pull request improves PSGallery publishing diagnostics with package-aware messages for authentication, permission, and conflict errors.

Changes:

  • Exposes the canonical PSGallery URI internally.
  • Extracts package identity from .nupkg files.
  • Adds custom handling for 401, 403, and 409 responses.
File summaries
File Summary
src/code/RepositorySettings.cs Makes the PSGallery URI available to publishing logic.
src/code/PublishHelper.cs Adds package-aware error handling. Findings include incorrect 401 messaging, missing message assertions for 401/403/409 responses, trailing-slash URI normalization, and duplicate terminal periods in two diagnostics.
Review details

Suppressed comments (3)

src/code/PublishHelper.cs:811

  • This renders modified.)., with two terminal periods, so the new duplicate-package diagnostic is malformed. Keep only one terminal period in this reason phrase.
                                $"(A package with id '{packageName}' and version '{packageVersion}' already exists and cannot be modified.).",

src/code/PublishHelper.cs:787

  • This renders package.)., with two terminal periods, so the diagnostic is malformed and does not preserve a clean PSGallery reason phrase. Keep the period either inside the parenthesized reason or after the closing parenthesis, not both.
                            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:709

  • This trims the URI only for isPSGallery; publishLocation was already computed from the untrimmed repoUri at line 670. A PSGallery URI ending in / (which can be registered with -ApiVersion V2) is therefore still published to /api/v2/ instead of /api/v2/package, while this branch treats it as PSGallery. Normalize before constructing publishLocation and reuse that value for both operations.
                string normalizedRepoUri = repoUri.TrimEnd('/');
                bool isPSGallery = normalizedRepoUri.Equals(RepositorySettings.PSGalleryRepoUri, StringComparison.OrdinalIgnoreCase);
  • 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
Comment on lines +746 to +749
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
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)."),
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