|
| 1 | +using SharePointPnP.PowerShell.CmdletHelpAttributes; |
| 2 | +using SharePointPnP.PowerShell.Commands.Model; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Management.Automation; |
| 6 | + |
| 7 | +namespace SharePointPnP.PowerShell.Commands.Base |
| 8 | +{ |
| 9 | + [Cmdlet(VerbsCommon.Get, "PnPException")] |
| 10 | + [CmdletHelp("Returns the last exception that occured", |
| 11 | + @"Returns the last exception which can be used while debugging PnP Cmdlets", |
| 12 | + Category = CmdletHelpCategory.Base)] |
| 13 | + [CmdletExample( |
| 14 | + Code = @"PS:> Get-PnPException", |
| 15 | + Remarks = "Returns the last exception", |
| 16 | + SortOrder = 1)] |
| 17 | + [CmdletExample( |
| 18 | + Code = @"PS:> Get-PnPException -All", |
| 19 | + Remarks = "Returns all exceptions that occurred", |
| 20 | + SortOrder = 2)] |
| 21 | + public class GetException : PSCmdlet |
| 22 | + { |
| 23 | + [Parameter(Mandatory = false, HelpMessage = "Show all exceptions")] |
| 24 | + public SwitchParameter All; |
| 25 | + |
| 26 | + protected override void ProcessRecord() |
| 27 | + { |
| 28 | + var exceptions = (ArrayList)this.SessionState.PSVariable.Get("error").Value; |
| 29 | + if (exceptions.Count > 0) |
| 30 | + { |
| 31 | + var output = new List<PnPException>(); |
| 32 | + if (All.IsPresent) |
| 33 | + { |
| 34 | + foreach (ErrorRecord exception in exceptions) |
| 35 | + { |
| 36 | + output.Add(new PnPException() { Message = exception.Exception.Message, Stacktrace = exception.Exception.StackTrace, ScriptLineNumber = exception.InvocationInfo.ScriptLineNumber, InvocationInfo = exception.InvocationInfo }); |
| 37 | + } |
| 38 | + } |
| 39 | + else |
| 40 | + { |
| 41 | + var exception = (ErrorRecord)exceptions[0]; |
| 42 | + output.Add(new PnPException() { Message = exception.Exception.Message, Stacktrace = exception.Exception.StackTrace, ScriptLineNumber = exception.InvocationInfo.ScriptLineNumber, InvocationInfo = exception.InvocationInfo }); |
| 43 | + } |
| 44 | + WriteObject(output, true); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments