Add custom exception messages for PSGallery - #2034
Open
shammu1 wants to merge 1 commit into
Open
Conversation
shammu1
requested review from
Sydney Smith (SydneyhSmith),
Aditya Patwardhan (adityapatwardhan),
alerickson and
Anam Navied (anamnavi)
as code owners
September 11, 2026 22:12
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Collaborator
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
🟡 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
.nupkgfiles. - 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;publishLocationwas already computed from the untrimmedrepoUriat 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 constructingpublishLocationand 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 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)."), |
| 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)."), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.