Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for -ErrorAction:Stop #2288

Merged
merged 4 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added support for `DisplayNamesOfFileViewers` and `DisplayNamesOfFileViewersInSpo` properties in `Get-PnPTenant` and `Set-PnPTenant` cmdlets to show/hide viewers in property pane for a file. [#2271](https://github.com/pnp/powershell/pull/2271)
- Added `MailEnabled`, `PreferredDataLocation`, `PreferredLanguage` and `SecurityEnabled` parameters to `New-PnPMicrosoft365Group` cmdlet. [#2268](https://github.com/pnp/powershell/pull/2268)
- Added `-DraftVersionVisibility` parameter to the `Set-PnPList` cmdlet to specify draft item security for list items. [#2285](https://github.com/pnp/powershell/pull/2285)
- Added support for `-ErrorAction:Stop` to PnP PowerShell cmdlets. Notice that if you were using this in combination with the specific try/catch [System.Management.Automation.PSInvalidOperationException], it will no longer catch the exception. It will throw an `System.Management.Automation.ErrorRecord` exception instead. Remove the `-ErrorAction:Stop` parameter from your cmdlet or catch this new exception type to avoid this behavior. [#2288](https://github.com/pnp/powershell/pull/2288)

### Changed
- Changed to no longer require https:// to be prefixed when using `Connect-PnPOnline -Url tenant.sharepoint.com` [#2139](https://github.com/pnp/powershell/pull/2139)
Expand Down
48 changes: 48 additions & 0 deletions src/Commands/Base/PnPConnectedCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,53 @@ protected void BeginProcessing(bool skipConnectedValidation)
throw new InvalidOperationException(Properties.Resources.NoConnection);
}
}

protected override void ProcessRecord()
{
try
{
ExecuteCmdlet();
}
catch (PipelineStoppedException)
{
// Don't swallow pipeline stopped exception, it makes select-object work weird
throw;
}
catch (Exception ex)
{
string errorMessage;
switch (ex)
{
case PnP.PowerShell.Commands.Model.Graph.GraphException gex:
errorMessage = gex.Message;
break;

case PnP.Core.SharePointRestServiceException rex:
errorMessage = (rex.Error as PnP.Core.SharePointRestError).Message;
break;

default:
errorMessage = ex.Message;
break;
}

// For backwards compatibility we will throw the exception as a PSInvalidOperationException if -ErrorAction:Stop has NOT been specified
if (!ParameterSpecified("ErrorAction") || MyInvocation.BoundParameters["ErrorAction"].ToString().ToLowerInvariant() != "stop")
{
throw new PSInvalidOperationException(errorMessage);
}

Connection.RestoreCachedContext(Connection.Url);
ex.Data["CorrelationId"] = Connection.Context.TraceCorrelationId;
ex.Data["TimeStampUtc"] = DateTime.UtcNow;
var errorDetails = new ErrorDetails(errorMessage);

errorDetails.RecommendedAction = "Use Get-PnPException for more details.";
var errorRecord = new ErrorRecord(ex, "EXCEPTION", ErrorCategory.WriteError, null);
errorRecord.ErrorDetails = errorDetails;

WriteError(errorRecord);
}
}
}
}
39 changes: 5 additions & 34 deletions src/Commands/Base/PnPSharePointCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,43 +102,14 @@ protected override void BeginProcessing()

protected override void ProcessRecord()
{
try
var tag = Connection.PnPVersionTag + ":" + MyInvocation.MyCommand.Name;
if (tag.Length > 32)
{
var tag = Connection.PnPVersionTag + ":" + MyInvocation.MyCommand.Name;
if (tag.Length > 32)
{
tag = tag.Substring(0, 32);
}
ClientContext.ClientTag = tag;

ExecuteCmdlet();
}
catch (PipelineStoppedException)
{
// Don't swallow pipeline stopped exception, it makes select-object work weird
throw;
}
catch (PnP.Core.SharePointRestServiceException ex)
{
throw new PSInvalidOperationException((ex.Error as PnP.Core.SharePointRestError).Message);
}
catch (PnP.PowerShell.Commands.Model.Graph.GraphException gex)
{
throw new PSInvalidOperationException((gex.Message));
tag = tag.Substring(0, 32);
}
catch (Exception ex)
{
Connection.RestoreCachedContext(Connection.Url);
ex.Data["CorrelationId"] = Connection.Context.TraceCorrelationId;
ex.Data["TimeStampUtc"] = DateTime.UtcNow;
var errorDetails = new ErrorDetails(ex.Message);

errorDetails.RecommendedAction = "Use Get-PnPException for more details.";
var errorRecord = new ErrorRecord(ex, "EXCEPTION", ErrorCategory.WriteError, null);
errorRecord.ErrorDetails = errorDetails;
ClientContext.ClientTag = tag;

WriteError(errorRecord);
}
base.ProcessRecord();
}

protected override void EndProcessing()
Expand Down