本文整理汇总了C#中System.Management.Automation.ErrorRecord.SetInvocationInfo方法的典型用法代码示例。如果您正苦于以下问题:C# ErrorRecord.SetInvocationInfo方法的具体用法?C# ErrorRecord.SetInvocationInfo怎么用?C# ErrorRecord.SetInvocationInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.ErrorRecord
的用法示例。
在下文中一共展示了ErrorRecord.SetInvocationInfo方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RuntimeException
internal RuntimeException(ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string errorIdAndResourceId,
string message,
Exception innerException)
: base(message, innerException)
{
SetErrorCategory(errorCategory);
SetErrorId(errorIdAndResourceId);
if ((errorPosition == null) && (invocationInfo != null))
{
errorPosition = invocationInfo.ScriptPosition;
}
if (invocationInfo == null) return;
_errorRecord = new ErrorRecord(
new ParentContainsErrorRecordException(this),
_errorId,
_errorCategory,
_targetObject);
_errorRecord.SetInvocationInfo(new InvocationInfo(invocationInfo.MyCommand, errorPosition));
}
示例2: ValidateCompatibleLanguageMode
/// <summary>
/// Ensures that the provided script block is compatible with the current language mode - to
/// be used when a script block is being dotted.
/// </summary>
/// <param name="scriptBlock">The script block being dotted</param>
/// <param name="languageMode">The current language mode</param>
/// <param name="invocationInfo">The invocation info about the command</param>
protected static void ValidateCompatibleLanguageMode(ScriptBlock scriptBlock,
PSLanguageMode languageMode,
InvocationInfo invocationInfo)
{
// If we are in a constrained language mode (Core or Restricted), block it.
// This goes both ways:
// - Can't dot something from a more permissive mode, since that would probably expose
// functions that were never designed to handle untrusted data.
// - Can't dot something from a less permissive mode, since that might introduce tainted
// data into the current scope.
if ((scriptBlock.LanguageMode.HasValue) &&
(scriptBlock.LanguageMode != languageMode) &&
((languageMode == PSLanguageMode.RestrictedLanguage) ||
(languageMode == PSLanguageMode.ConstrainedLanguage)))
{
ErrorRecord errorRecord = new ErrorRecord(
new NotSupportedException(
DiscoveryExceptions.DotSourceNotSupported),
"DotSourceNotSupported",
ErrorCategory.InvalidOperation,
null);
errorRecord.SetInvocationInfo(invocationInfo);
throw new CmdletInvocationException(errorRecord);
}
}
示例3: ManageInvocationException
//.........这里部分代码省略.........
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;
}
}
示例4: ManageInvocationException
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;
}
示例5: ValidateCompatibleLanguageMode
protected static void ValidateCompatibleLanguageMode(ScriptBlock scriptBlock, PSLanguageMode languageMode, InvocationInfo invocationInfo)
{
if (scriptBlock.LanguageMode.HasValue)
{
PSLanguageMode? nullable2 = scriptBlock.LanguageMode;
PSLanguageMode mode = languageMode;
if (((((PSLanguageMode) nullable2.GetValueOrDefault()) != mode) || !nullable2.HasValue) && ((languageMode == PSLanguageMode.RestrictedLanguage) || (languageMode == PSLanguageMode.ConstrainedLanguage)))
{
ErrorRecord errorRecord = new ErrorRecord(new NotSupportedException(DiscoveryExceptions.DotSourceNotSupported), "DotSourceNotSupported", ErrorCategory.InvalidOperation, null);
errorRecord.SetInvocationInfo(invocationInfo);
throw new CmdletInvocationException(errorRecord);
}
}
}
示例6: ValidateCompatibleLanguageMode
/// <summary>
/// Ensures that the provided script block is compatible with the current language mode - to
/// be used when a script block is being dotted.
/// </summary>
/// <param name="scriptBlock">The script block being dotted</param>
/// <param name="languageMode">The current language mode</param>
/// <param name="invocationInfo">The invocation info about the command</param>
protected static void ValidateCompatibleLanguageMode(ScriptBlock scriptBlock,
PSLanguageMode languageMode,
InvocationInfo invocationInfo)
{
// If we are in a constrained language mode (Core or Restricted), block it.
// We are currently restricting in one direction:
// - Can't dot something from a more permissive mode, since that would probably expose
// functions that were never designed to handle untrusted data.
// This function won't be called for NoLanguage mode so the only direction checked is trusted
// (FullLanguage mode) script running in a constrained/restricted session.
if ((scriptBlock.LanguageMode.HasValue) &&
(scriptBlock.LanguageMode != languageMode) &&
((languageMode == PSLanguageMode.RestrictedLanguage) ||
(languageMode == PSLanguageMode.ConstrainedLanguage)))
{
// Finally check if script block is really just PowerShell commands plus parameters.
// If so then it is safe to dot source across language mode boundaries.
bool isSafeToDotSource = false;
try
{
scriptBlock.GetPowerShell();
isSafeToDotSource = true;
}
catch (Exception e)
{
CheckForSevereException(e);
}
if (!isSafeToDotSource)
{
ErrorRecord errorRecord = new ErrorRecord(
new NotSupportedException(
DiscoveryExceptions.DotSourceNotSupported),
"DotSourceNotSupported",
ErrorCategory.InvalidOperation,
null);
errorRecord.SetInvocationInfo(invocationInfo);
throw new CmdletInvocationException(errorRecord);
}
}
}
示例7: WriteInputObjectError
/// <summary>
/// Writes an ErrorRecord to the commands error pipe because the specified
/// input object was not bound to the command.
/// </summary>
///
/// <param name="inputObject">
/// The pipeline input object that was not bound.
/// </param>
///
/// <param name="resourceString">
/// The error message.
/// </param>
///
/// <param name="errorId">
/// The resource ID of the error message is also used as error ID
/// of the ErrorRecord.
/// </param>
///
/// <param name="args">
/// Additional arguments to be formatted into the error message that represented in <paramref name="resourceString"/>.
/// </param>
///
private void WriteInputObjectError(
object inputObject,
string resourceString,
string errorId,
params object[] args)
{
Type inputObjectType = (inputObject == null) ? null : inputObject.GetType();
ParameterBindingException bindingException = new ParameterBindingException(
ErrorCategory.InvalidArgument,
this.Command.MyInvocation,
null,
null,
null,
inputObjectType,
resourceString,
errorId,
args);
ErrorRecord errorRecord =
new ErrorRecord(
bindingException,
errorId,
ErrorCategory.InvalidArgument,
inputObject);
errorRecord.SetInvocationInfo(this.Command.MyInvocation);
this.commandRuntime._WriteErrorSkipAllowCheck(errorRecord);
} // WriteIgnoredInputObjectError
示例8: ThrowTerminatingError
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);
}
示例9: ThrowTerminatingError
/// <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);
}
示例10: WriteInputObjectError
private void WriteInputObjectError(object inputObject, string resourceAndErrorId, params object[] args)
{
Type typeSpecified = (inputObject == null) ? null : inputObject.GetType();
ParameterBindingException exception = new ParameterBindingException(ErrorCategory.InvalidArgument, base.Command.MyInvocation, null, null, null, typeSpecified, "ParameterBinderStrings", resourceAndErrorId, args);
ErrorRecord errorRecord = new ErrorRecord(exception, resourceAndErrorId, ErrorCategory.InvalidArgument, inputObject);
errorRecord.SetInvocationInfo(base.Command.MyInvocation);
base.commandRuntime._WriteErrorSkipAllowCheck(errorRecord, null);
}