internal PipelineStoppedException ManageInvocationException(Exception e) { PipelineStoppedException exception4; try { if (this.Command != null) { ProviderInvocationException innerException = e as ProviderInvocationException; if (innerException != null) { e = new CmdletProviderInvocationException(innerException, this.Command.MyInvocation); } else if (((!(e is PipelineStoppedException) && !(e is CmdletInvocationException)) && (!(e is ActionPreferenceStopException) && !(e is HaltCommandException))) && (!(e is FlowControlException) && !(e is ScriptCallDepthException))) { RuntimeException exception2 = e as RuntimeException; if ((exception2 == null) || !exception2.WasThrownFromThrowStatement) { e = new CmdletInvocationException(e, this.Command.MyInvocation); } } if (this.commandRuntime.UseTransaction != 0) { bool flag = false; for (Exception exception3 = e; exception3 != null; exception3 = exception3.InnerException) { if (exception3 is TimeoutException) { flag = true; break; } } if (flag) { ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(TransactionStrings.TransactionTimedOut), "TRANSACTION_TIMEOUT", ErrorCategory.InvalidOperation, e); errorRecord.SetInvocationInfo(this.Command.MyInvocation); e = new CmdletInvocationException(errorRecord); } if (this._context.TransactionManager.HasTransaction && (this._context.TransactionManager.RollbackPreference != RollbackSeverity.Never)) { this.Context.TransactionManager.Rollback(true); } } return((PipelineStoppedException)this.commandRuntime.ManageException(e)); } exception4 = new PipelineStoppedException(); } catch (Exception) { throw; } return(exception4); }
private void Init(CmdletInfo cmdletInformation) { Cmdlet cmdlet = (Cmdlet)null; Exception exception = (Exception)null; string errorIdAndResourceId = "CmdletNotFoundException"; try { cmdlet = (Cmdlet)Activator.CreateInstance(cmdletInformation.ImplementingType); } catch (TargetInvocationException ex) { CommandProcessor.tracer.TraceException((Exception)ex); CmdletInvocationException invocationException = new CmdletInvocationException(ex.InnerException, (InvocationInfo)null); MshLog.LogCommandHealthEvent(this.context, (Exception)invocationException, Severity.Warning); throw invocationException; } catch (MemberAccessException ex) { exception = (Exception)ex; } catch (TypeLoadException ex) { exception = (Exception)ex; } catch (InvalidCastException ex) { exception = (Exception)ex; errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType"; } catch (Exception ex) { CommandProcessorBase.CheckForSevereException(ex); CommandProcessor.tracer.TraceException(ex); MshLog.LogCommandHealthEvent(this.context, ex, Severity.Warning); throw; } if (exception != null) { CommandProcessor.tracer.TraceException(exception); MshLog.LogCommandHealthEvent(this.context, exception, Severity.Warning); CommandNotFoundException notFoundException = new CommandNotFoundException(cmdletInformation.Name, exception, errorIdAndResourceId, new object[1] { (object)exception.Message }); CommandProcessor.tracer.TraceException((Exception)notFoundException); throw notFoundException; } this.Command = (InternalCommand)cmdlet; this.InitCommon(); }
private void Init(CmdletInfo cmdletInformation) { Cmdlet cmdlet = null; Exception exception = null; string str = null; string cmdletDoesNotDeriveFromCmdletType = null; try { cmdlet = ConstructInstance(cmdletInformation.ImplementingType); if (cmdlet == null) { exception = new InvalidCastException(); str = "CmdletDoesNotDeriveFromCmdletType"; cmdletDoesNotDeriveFromCmdletType = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType; } } catch (MemberAccessException exception2) { exception = exception2; } catch (TypeLoadException exception3) { exception = exception3; } catch (Exception exception4) { CommandProcessorBase.CheckForSevereException(exception4); CmdletInvocationException exception5 = new CmdletInvocationException(exception4, null); MshLog.LogCommandHealthEvent(base._context, exception5, Severity.Warning); throw exception5; } if (exception != null) { MshLog.LogCommandHealthEvent(base._context, exception, Severity.Warning); CommandNotFoundException exception6 = new CommandNotFoundException(cmdletInformation.Name, exception, str ?? "CmdletNotFoundException", cmdletDoesNotDeriveFromCmdletType ?? DiscoveryExceptions.CmdletNotFoundException, new object[] { exception.Message }); throw exception6; } base.Command = cmdlet; base.CommandScope = base.Context.EngineSessionState.CurrentScope; this.InitCommon(); }
/// <summary> /// Initializes the command's request object. /// </summary> /// <param name="cmdletInformation"> /// The information about the cmdlet. /// </param> /// <exception cref="CmdletInvocationException"> /// If the constructor for the cmdlet threw an exception. /// </exception> /// <exception cref="MemberAccessException"> /// The type referenced by <paramref name="cmdletInformation"/> refered to an /// abstract type or them member was invoked via a late-binding mechanism. /// </exception> /// <exception cref="TypeLoadException"> /// If <paramref name="cmdletInformation"/> refers to a type that is invalid. /// </exception> private void Init(CmdletInfo cmdletInformation) { Diagnostics.Assert(cmdletInformation != null, "Constructor should throw exception if LookupCommand returned null."); Cmdlet newCmdlet = null; Exception initError = null; string errorIdAndResourceId = null; string resourceStr = null; try { // Create the request object newCmdlet = ConstructInstance(cmdletInformation.ImplementingType); if (newCmdlet == null) { // We could test the inheritance before constructing, but that's // expensive. Much cheaper to just check for null. initError = new InvalidCastException(); errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType"; resourceStr = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType; } } catch (MemberAccessException memberAccessException) { initError = memberAccessException; } catch (TypeLoadException typeLoadException) { initError = typeLoadException; } catch (Exception e) // Catch-all OK, 3rd party callout. { // We don't have a Command or InvocationInfo at this point, // since the command failed to initialize. var commandException = new CmdletInvocationException(e, null); // Log a command health event MshLog.LogCommandHealthEvent( this._context, commandException, Severity.Warning); throw commandException; } if (initError != null) { // Log a command health event MshLog.LogCommandHealthEvent( this._context, initError, Severity.Warning); CommandNotFoundException exception = new CommandNotFoundException( cmdletInformation.Name, initError, errorIdAndResourceId ?? "CmdletNotFoundException", resourceStr ?? DiscoveryExceptions.CmdletNotFoundException, initError.Message); throw exception; } this.Command = newCmdlet; this.CommandScope = Context.EngineSessionState.CurrentScope; InitCommon(); }
/// <summary> /// Wraps the exception which occurred during cmdlet invocation, /// stores that as the exception to be returned from /// PipelineProcessor.SynchronousExecute, and writes it to /// the error variable. /// </summary> /// <param name="e"> /// The exception to wrap in a CmdletInvocationException or /// CmdletProviderInvocationException. /// </param> /// <returns> /// Always returns PipelineStoppedException. The caller should /// throw this exception. /// </returns> /// <remarks> /// Almost all exceptions which occur during pipeline invocation /// are wrapped in CmdletInvocationException before they are stored /// in the pipeline. However, there are several exceptions: /// /// AccessViolationException, StackOverflowException: /// These are considered to be such severe errors that we /// FailFast the process immediately. /// /// ProviderInvocationException: In this case, we assume that the /// cmdlet is get-item or the like, a thin wrapper around the /// provider API. We discard the original ProviderInvocationException /// and re-wrap its InnerException (the real error) in /// CmdletProviderInvocationException. This makes it easier to reach /// the real error. /// /// CmdletInvocationException, ActionPreferenceStopException: /// This indicates that the cmdlet itself ran a command which failed. /// We could go ahead and wrap the original exception in multiple /// layers of CmdletInvocationException, but this makes it difficult /// for the caller to access the root problem, plus the serialization /// layer might not communicate properties beyond some fixed depth. /// Instead, we choose to not re-wrap the exception. /// /// PipelineStoppedException: This could mean one of two things. /// It usually means that this pipeline has already stopped, /// in which case the pipeline already stores the original error. /// It could also mean that the cmdlet ran a command which was /// stopped by CTRL-C etc, in which case we choose not to /// re-wrap the exception as with CmdletInvocationException. /// </remarks> internal PipelineStoppedException ManageInvocationException(Exception e) { try { if (Command != null) { do // false loop { ProviderInvocationException pie = e as ProviderInvocationException; if (pie != null) { // If a ProviderInvocationException occurred, // discard the ProviderInvocationException and // re-wrap in CmdletProviderInvocationException e = new CmdletProviderInvocationException( pie, Command.MyInvocation); break; } // 1021203-2005/05/09-JonN // HaltCommandException will cause the command // to stop, but not be reported as an error. // 906445-2005/05/16-JonN // FlowControlException should not be wrapped if (e is PipelineStoppedException || e is CmdletInvocationException || e is ActionPreferenceStopException || e is HaltCommandException || e is FlowControlException || e is ScriptCallDepthException) { // do nothing; do not rewrap these exceptions break; } RuntimeException rte = e as RuntimeException; if (rte != null && rte.WasThrownFromThrowStatement) { // do not rewrap a script based throw break; } // wrap all other exceptions e = new CmdletInvocationException( e, Command.MyInvocation); } while (false); // commandRuntime.ManageException will always throw PipelineStoppedException // Otherwise, just return this exception... // If this exception happened in a transacted cmdlet, // rollback the transaction if (commandRuntime.UseTransaction) { // The "transaction timed out" exception is // exceedingly obtuse. We clarify things here. bool isTimeoutException = false; Exception tempException = e; while (tempException != null) { if (tempException is System.TimeoutException) { isTimeoutException = true; break; } tempException = tempException.InnerException; } if (isTimeoutException) { ErrorRecord errorRecord = new ErrorRecord( new InvalidOperationException( TransactionStrings.TransactionTimedOut), "TRANSACTION_TIMEOUT", ErrorCategory.InvalidOperation, e); errorRecord.SetInvocationInfo(Command.MyInvocation); e = new CmdletInvocationException(errorRecord); } // Rollback the transaction in the case of errors. if ( _context.TransactionManager.HasTransaction && _context.TransactionManager.RollbackPreference != RollbackSeverity.Never ) { Context.TransactionManager.Rollback(true); } } return((PipelineStoppedException)this.commandRuntime.ManageException(e)); } // Upstream cmdlets see only that execution stopped // This should only happen if Command is null return(new PipelineStoppedException()); } catch (Exception) { // this method should not throw exceptions; warn about any violations on checked builds and re-throw Diagnostics.Assert(false, "This method should not throw exceptions!"); throw; } }
/// <summary> /// Wraps the exception which occurred during cmdlet invocation, /// stores that as the exception to be returned from /// PipelineProcessor.SynchronousExecute, and writes it to /// the error variable. /// </summary> /// /// <param name="e"> /// The exception to wrap in a CmdletInvocationException or /// CmdletProviderInvocationException. /// </param> /// /// <returns> /// Always returns PipelineStoppedException. The caller should /// throw this exception. /// </returns> /// /// <remarks> /// Almost all exceptions which occur during pipeline invocation /// are wrapped in CmdletInvocationException before they are stored /// in the pipeline. However, there are several exceptions: /// /// AccessViolationException, StackOverflowException: /// These are considered to be such severe errors that we /// FailFast the process immediately. /// /// ProviderInvocationException: In this case, we assume that the /// cmdlet is get-item or the like, a thin wrapper around the /// provider API. We discard the original ProviderInvocationException /// and re-wrap its InnerException (the real error) in /// CmdletProviderInvocationException. This makes it easier to reach /// the real error. /// /// CmdletInvocationException, ActionPreferenceStopException: /// This indicates that the cmdlet itself ran a command which failed. /// We could go ahead and wrap the original exception in multiple /// layers of CmdletInvocationException, but this makes it difficult /// for the caller to access the root problem, plus the serialization /// layer might not communicate properties beyond some fixed depth. /// Instead, we choose to not re-wrap the exception. /// /// PipelineStoppedException: This could mean one of two things. /// It usually means that this pipeline has already stopped, /// in which case the pipeline already stores the original error. /// It could also mean that the cmdlet ran a command which was /// stopped by CTRL-C etc, in which case we choose not to /// re-wrap the exception as with CmdletInvocationException. /// </remarks> internal PipelineStoppedException ManageInvocationException(Exception e) { try { if (null != Command) { do // false loop { ProviderInvocationException pie = e as ProviderInvocationException; if (pie != null) { // If a ProviderInvocationException occurred, // discard the ProviderInvocationException and // re-wrap in CmdletProviderInvocationException e = new CmdletProviderInvocationException( pie, Command.MyInvocation); break; } // 1021203-2005/05/09-JonN // HaltCommandException will cause the command // to stop, but not be reported as an error. // 906445-2005/05/16-JonN // FlowControlException should not be wrapped if (e is PipelineStoppedException || e is CmdletInvocationException || e is ActionPreferenceStopException || e is HaltCommandException || e is FlowControlException || e is ScriptCallDepthException) { // do nothing; do not rewrap these exceptions break; } RuntimeException rte = e as RuntimeException; if (rte != null && rte.WasThrownFromThrowStatement) { // do not rewrap a script based throw break; } // wrap all other exceptions e = new CmdletInvocationException( e, Command.MyInvocation); } while (false); // commandRuntime.ManageException will always throw PipelineStoppedException // Otherwise, just return this exception... // If this exception happened in a transacted cmdlet, // rollback the transaction if (commandRuntime.UseTransaction) { // The "transaction timed out" exception is // exceedingly obtuse. We clarify things here. bool isTimeoutException = false; Exception tempException = e; while (tempException != null) { if (tempException is System.TimeoutException) { isTimeoutException = true; break; } tempException = tempException.InnerException; } if (isTimeoutException) { ErrorRecord errorRecord = new ErrorRecord( new InvalidOperationException( TransactionStrings.TransactionTimedOut), "TRANSACTION_TIMEOUT", ErrorCategory.InvalidOperation, e); errorRecord.SetInvocationInfo(Command.MyInvocation); e = new CmdletInvocationException(errorRecord); } // Rollback the transaction in the case of errors. if ( _context.TransactionManager.HasTransaction && _context.TransactionManager.RollbackPreference != RollbackSeverity.Never ) { Context.TransactionManager.Rollback(true); } } return (PipelineStoppedException)this.commandRuntime.ManageException(e); } // Upstream cmdlets see only that execution stopped // This should only happen if Command is null return new PipelineStoppedException(); } catch (Exception) { // this method should not throw exceptions; warn about any violations on checked builds and re-throw Diagnostics.Assert(false, "This method should not throw exceptions!"); throw; } }
internal PipelineStoppedException ManageInvocationException(Exception e) { PipelineStoppedException exception4; try { if (this.Command != null) { ProviderInvocationException innerException = e as ProviderInvocationException; if (innerException != null) { e = new CmdletProviderInvocationException(innerException, this.Command.MyInvocation); } else if (((!(e is PipelineStoppedException) && !(e is CmdletInvocationException)) && (!(e is ActionPreferenceStopException) && !(e is HaltCommandException))) && (!(e is FlowControlException) && !(e is ScriptCallDepthException))) { RuntimeException exception2 = e as RuntimeException; if ((exception2 == null) || !exception2.WasThrownFromThrowStatement) { e = new CmdletInvocationException(e, this.Command.MyInvocation); } } if (this.commandRuntime.UseTransaction != 0) { bool flag = false; for (Exception exception3 = e; exception3 != null; exception3 = exception3.InnerException) { if (exception3 is TimeoutException) { flag = true; break; } } if (flag) { ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(TransactionStrings.TransactionTimedOut), "TRANSACTION_TIMEOUT", ErrorCategory.InvalidOperation, e); errorRecord.SetInvocationInfo(this.Command.MyInvocation); e = new CmdletInvocationException(errorRecord); } if (this._context.TransactionManager.HasTransaction && (this._context.TransactionManager.RollbackPreference != RollbackSeverity.Never)) { this.Context.TransactionManager.Rollback(true); } } return (PipelineStoppedException) this.commandRuntime.ManageException(e); } exception4 = new PipelineStoppedException(); } catch (Exception) { throw; } return exception4; }
private void DisposeCommands() { this.stopping = true; this.FlushLog(); if (this._commands != null) { for (int i = 0; i < this._commands.Count; i++) { CommandProcessorBase base2 = this._commands[i]; if (base2 != null) { try { base2.CommandRuntime.RemoveVariableListsInPipe(); base2.Dispose(); } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); InvocationInfo myInvocation = null; if (base2.Command != null) { myInvocation = base2.Command.MyInvocation; } ProviderInvocationException innerException = exception as ProviderInvocationException; if (innerException != null) { exception = new CmdletProviderInvocationException(innerException, myInvocation); } else { exception = new CmdletInvocationException(exception, myInvocation); MshLog.LogCommandHealthEvent(base2.Command.Context, exception, Severity.Warning); } this.RecordFailure(exception, base2.Command); } } } } this._commands = null; if (this._redirectionPipes != null) { foreach (PipelineProcessor processor in this._redirectionPipes) { try { if (processor != null) { processor.Dispose(); } } catch (Exception exception3) { CommandProcessorBase.CheckForSevereException(exception3); } } } this._redirectionPipes = null; }
/// <summary> /// Initializes the command's request object /// </summary> /// /// <param name="cmdletInformation"> /// The information about the cmdlet. /// </param> /// /// <exception cref="CmdletInvocationException"> /// If the constructor for the cmdlet threw an exception. /// </exception> /// /// <exception cref="MemberAccessException"> /// The type referenced by <paramref name="cmdletInformation"/> refered to an /// abstract type or them member was invoked via a late-binding mechanism. /// </exception> /// /// <exception cref="TypeLoadException"> /// If <paramref name="cmdletInformation"/> refers to a type that is invalid. /// </exception> /// private void Init(CmdletInfo cmdletInformation) { Diagnostics.Assert(cmdletInformation != null, "Constructor should throw exception if LookupCommand returned null."); Cmdlet newCmdlet = null; Exception initError = null; string errorIdAndResourceId = null; string resourceStr = null; try { // Create the request object newCmdlet = ConstructInstance(cmdletInformation.ImplementingType); if (newCmdlet == null) { // We could test the inheritance before constructing, but that's // expensive. Much cheaper to just check for null. initError = new InvalidCastException(); errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType"; resourceStr = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType; } } catch (MemberAccessException memberAccessException) { initError = memberAccessException; } catch (TypeLoadException typeLoadException) { initError = typeLoadException; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); // We don't have a Command or InvocationInfo at this point, // since the command failed to initialize. var commandException = new CmdletInvocationException(e, null); // Log a command health event MshLog.LogCommandHealthEvent( this._context, commandException, Severity.Warning); throw commandException; } if (null != initError) { // Log a command health event MshLog.LogCommandHealthEvent( this._context, initError, Severity.Warning); CommandNotFoundException exception = new CommandNotFoundException( cmdletInformation.Name, initError, errorIdAndResourceId ?? "CmdletNotFoundException", resourceStr ?? DiscoveryExceptions.CmdletNotFoundException, initError.Message); throw exception; } this.Command = newCmdlet; this.CommandScope = Context.EngineSessionState.CurrentScope; InitCommon(); }
public void ThrowTerminatingError(ErrorRecord errorRecord) { this.ThrowIfStopping(); if (errorRecord == null) { throw PSTraceSource.NewArgumentNullException("errorRecord"); } errorRecord.SetInvocationInfo(this.MyInvocation); if ((errorRecord.ErrorDetails != null) && (errorRecord.ErrorDetails.TextLookupError != null)) { Exception textLookupError = errorRecord.ErrorDetails.TextLookupError; errorRecord.ErrorDetails.TextLookupError = null; MshLog.LogCommandHealthEvent(this.context, textLookupError, Severity.Warning); } if ((errorRecord.Exception != null) && string.IsNullOrEmpty(errorRecord.Exception.StackTrace)) { try { throw errorRecord.Exception; } catch (Exception) { } } CmdletInvocationException e = new CmdletInvocationException(errorRecord); throw this.ManageException(e); }
/// <summary> /// Implementation of ThrowTerminatingError. /// </summary> /// <param name="errorRecord"> /// The error which caused the command to be terminated /// </param> /// <exception cref="PipelineStoppedException"> /// always /// </exception> /// <remarks> /// <see cref="System.Management.Automation.Cmdlet.ThrowTerminatingError"/> /// terminates the command, where /// <see cref="System.Management.Automation.ICommandRuntime.WriteError"/> /// allows the command to continue. /// /// The cmdlet can also terminate the command by simply throwing /// any exception. When the cmdlet's implementation of /// <see cref="System.Management.Automation.Cmdlet.ProcessRecord"/>, /// <see cref="System.Management.Automation.Cmdlet.BeginProcessing"/> or /// <see cref="System.Management.Automation.Cmdlet.EndProcessing"/> /// throws an exception, the Engine will always catch the exception /// and report it as a terminating error. /// However, it is preferred for the cmdlet to call /// <see cref="System.Management.Automation.Cmdlet.ThrowTerminatingError"/>, /// so that the additional information in /// <see cref="System.Management.Automation.ErrorRecord"/> /// is available. /// /// <see cref="System.Management.Automation.Cmdlet.ThrowTerminatingError"/> /// always throws /// <see cref="System.Management.Automation.PipelineStoppedException"/>, /// regardless of what error was specified in <paramref name="errorRecord"/>. /// The Cmdlet should generally just allow /// <see cref="System.Management.Automation.PipelineStoppedException"/>. /// to percolate up to the caller of /// <see cref="System.Management.Automation.Cmdlet.ProcessRecord"/>. /// etc. /// </remarks> public void ThrowTerminatingError(ErrorRecord errorRecord) { ThrowIfStopping(); if (null == errorRecord) { throw PSTraceSource.NewArgumentNullException("errorRecord"); } errorRecord.SetInvocationInfo(MyInvocation); if (null != errorRecord.ErrorDetails && null != errorRecord.ErrorDetails.TextLookupError) { Exception textLookupError = errorRecord.ErrorDetails.TextLookupError; errorRecord.ErrorDetails.TextLookupError = null; MshLog.LogCommandHealthEvent( Context, textLookupError, Severity.Warning); } // This code forces the stack trace and source fields to be populated if (null != errorRecord.Exception && String.IsNullOrEmpty(errorRecord.Exception.StackTrace)) { try { throw errorRecord.Exception; } catch (Exception) { // no need to worry about severe exceptions since // it wasn't really thrown originally } } CmdletInvocationException e = new CmdletInvocationException(errorRecord); // Code sees only that execution stopped throw ManageException(e); }