/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace System.Management.Automation
{
///
/// The exception thrown if the specified value can not be bound parameter of a command.
///
[Serializable]
public class ParameterBindingException : RuntimeException
{
#region Constructors
#region Preferred constructors
///
/// Constructs a ParameterBindingException.
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
///
///
///
///
/// The position for the command or parameter that caused the error.
/// If position is null, the one from the InvocationInfo is used.
///
///
///
///
/// The parameter on which binding caused the error.
///
///
///
///
/// The Type the parameter was expecting.
///
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
///
///
///
/// If or
/// is null or empty.
///
internal ParameterBindingException(
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(errorCategory, invocationInfo, errorPosition, errorId, null, null)
{
if (String.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
if (String.IsNullOrEmpty(errorId))
{
throw PSTraceSource.NewArgumentException("errorId");
}
_invocationInfo = invocationInfo;
if (_invocationInfo != null)
{
_commandName = invocationInfo.MyCommand.Name;
}
_parameterName = parameterName;
_parameterType = parameterType;
_typeSpecified = typeSpecified;
if ((errorPosition == null) && (_invocationInfo != null))
{
errorPosition = invocationInfo.ScriptPosition;
}
if (errorPosition != null)
{
_line = errorPosition.StartLineNumber;
_offset = errorPosition.StartColumnNumber;
}
_resourceString = resourceString;
_errorId = errorId;
if (args != null)
{
_args = args;
}
}
///
/// Constructs a ParameterBindingException
///
///
///
/// The inner exception.
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
/// If position is null, the one from the InvocationInfo is used.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If is null.
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingException(
Exception innerException,
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(errorCategory, invocationInfo, errorPosition, errorId, null, innerException)
{
if (invocationInfo == null)
{
throw PSTraceSource.NewArgumentNullException("invocationInfo");
}
if (String.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
if (String.IsNullOrEmpty(errorId))
{
throw PSTraceSource.NewArgumentException("errorId");
}
_invocationInfo = invocationInfo;
_commandName = invocationInfo.MyCommand.Name;
_parameterName = parameterName;
_parameterType = parameterType;
_typeSpecified = typeSpecified;
if (errorPosition == null)
{
errorPosition = invocationInfo.ScriptPosition;
}
if (errorPosition != null)
{
_line = errorPosition.StartLineNumber;
_offset = errorPosition.StartColumnNumber;
}
_resourceString = resourceString;
_errorId = errorId;
if (args != null)
{
_args = args;
}
}
///
///
///
///
///
///
///
internal ParameterBindingException(
Exception innerException,
ParameterBindingException pbex,
string resourceString,
params object[] args)
: base(String.Empty, innerException)
{
if (pbex == null)
{
throw PSTraceSource.NewArgumentNullException("pbex");
}
if (String.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
_invocationInfo = pbex.CommandInvocation;
if (_invocationInfo != null)
{
_commandName = _invocationInfo.MyCommand.Name;
}
IScriptExtent errorPosition = null;
if (_invocationInfo != null)
{
errorPosition = _invocationInfo.ScriptPosition;
}
_line = pbex.Line;
_offset = pbex.Offset;
_parameterName = pbex.ParameterName;
_parameterType = pbex.ParameterType;
_typeSpecified = pbex.TypeSpecified;
_errorId = pbex.ErrorId;
_resourceString = resourceString;
if (args != null)
{
_args = args;
}
base.SetErrorCategory(pbex.ErrorRecord._category);
base.SetErrorId(_errorId);
if (_invocationInfo != null)
{
base.ErrorRecord.SetInvocationInfo(new InvocationInfo(_invocationInfo.MyCommand, errorPosition));
}
}
#endregion Preferred constructors
#region serialization
///
/// Constructors a ParameterBindingException using serialized data.
///
///
///
/// serialization information
///
///
///
/// streaming context
///
protected ParameterBindingException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
_message = info.GetString("ParameterBindingException_Message");
_parameterName = info.GetString("ParameterName");
_line = info.GetInt64("Line");
_offset = info.GetInt64("Offset");
}
///
/// Serializes the exception
///
///
///
/// serialization information
///
///
///
/// streaming context
///
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new PSArgumentNullException("info");
}
base.GetObjectData(info, context);
info.AddValue("ParameterBindingException_Message", this.Message);
info.AddValue("ParameterName", _parameterName);
info.AddValue("Line", _line);
info.AddValue("Offset", _offset);
}
#endregion serialization
#region Do Not Use
///
/// Constructs a ParameterBindingException.
///
///
///
/// DO NOT USE!!!
///
public ParameterBindingException() : base() {; }
///
/// Constructors a ParameterBindingException
///
///
///
/// Message to be included in exception.
///
///
///
/// DO NOT USE!!!
///
public ParameterBindingException(string message) : base(message) { _message = message; }
///
/// Constructs a ParameterBindingException
///
///
///
/// Message to be included in the exception.
///
///
///
/// exception that led to this exception
///
///
///
/// DO NOT USE!!!
///
public ParameterBindingException(
string message,
Exception innerException)
: base(message, innerException)
{ _message = message; }
#endregion Do Not Use
#endregion Constructors
#region Properties
///
/// Gets the message for the exception
///
public override string Message
{
get { return _message ?? (_message = BuildMessage()); }
}
private string _message;
///
/// Gets the name of the parameter that the parameter binding
/// error was encountered on.
///
public string ParameterName
{
get
{
return _parameterName;
}
}
private string _parameterName = String.Empty;
///
/// Gets the type the parameter is expecting.
///
public Type ParameterType
{
get
{
return _parameterType;
}
}
private Type _parameterType;
///
/// Gets the Type that was specified as the parameter value
///
public Type TypeSpecified
{
get
{
return _typeSpecified;
}
}
private Type _typeSpecified;
///
/// Gets the errorId of this ParameterBindingException
///
public string ErrorId
{
get
{
return _errorId;
}
}
private string _errorId;
///
/// Gets the line in the script at which the error occurred.
///
public Int64 Line
{
get
{
return _line;
}
}
private Int64 _line = Int64.MinValue;
///
/// Gets the offset on the line in the script at which the error occurred.
///
public Int64 Offset
{
get
{
return _offset;
}
}
private Int64 _offset = Int64.MinValue;
///
/// Gets the invocation information about the command.
///
public InvocationInfo CommandInvocation
{
get
{
return _invocationInfo;
}
}
private InvocationInfo _invocationInfo;
#endregion Properties
#region private
private string _resourceString;
private object[] _args = new object[0];
private string _commandName;
private string BuildMessage()
{
object[] messageArgs = new object[0];
if (_args != null)
{
messageArgs = new object[_args.Length + 6];
messageArgs[0] = _commandName;
messageArgs[1] = _parameterName;
messageArgs[2] = _parameterType;
messageArgs[3] = _typeSpecified;
messageArgs[4] = _line;
messageArgs[5] = _offset;
_args.CopyTo(messageArgs, 6);
}
string result = String.Empty;
if (!String.IsNullOrEmpty(_resourceString))
{
result = StringUtil.Format(_resourceString, messageArgs);
}
return result;
}
#endregion Private
}
[Serializable]
internal class ParameterBindingValidationException : ParameterBindingException
{
#region Preferred constructors
///
/// Constructs a ParameterBindingValidationException
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingValidationException(
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
}
///
/// Constructs a ParameterBindingValidationException
///
///
///
/// The inner exception.
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If is null.
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingValidationException(
Exception innerException,
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
innerException,
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
ValidationMetadataException validationException = innerException as ValidationMetadataException;
if (validationException != null && validationException.SwallowException)
{
_swallowException = true;
}
}
#endregion Preferred constructors
#region serialization
///
/// Constructs a ParameterBindingValidationException from serialized data
///
///
///
/// serialization information
///
///
///
/// streaming context
///
protected ParameterBindingValidationException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
#endregion serialization
#region Property
///
/// Make the positional binding ignore this validation exception when it's set to true.
///
///
///
/// This property is only used internally in the positional binding phase
///
internal bool SwallowException
{
get { return _swallowException; }
}
private readonly bool _swallowException = false;
#endregion Property
}
[Serializable]
internal class ParameterBindingArgumentTransformationException : ParameterBindingException
{
#region Preferred constructors
///
/// Constructs a ParameterBindingArgumentTransformationException
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingArgumentTransformationException(
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
}
///
/// Constructs a ParameterBindingArgumentTransformationException
///
///
///
/// The inner exception.
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If is null.
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingArgumentTransformationException(
Exception innerException,
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
innerException,
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
}
#endregion Preferred constructors
#region serialization
///
/// Constructs a ParameterBindingArgumentTransformationException using serialized data
///
///
///
/// serialization information
///
///
///
/// streaming context
///
protected ParameterBindingArgumentTransformationException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
#endregion serialization
}
[Serializable]
internal class ParameterBindingParameterDefaultValueException : ParameterBindingException
{
#region Preferred constructors
///
/// Constructs a ParameterBindingParameterDefaultValueException
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingParameterDefaultValueException(
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
}
///
/// Constructs a ParameterBindingParameterDefaultValueException
///
///
///
/// The inner exception.
///
///
///
/// The category for the error.
///
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
///
/// The position for the command or parameter that caused the error.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
///
/// The format string for the exception message.
///
///
///
/// The error ID.
///
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
///
/// If is null.
///
///
///
/// If or
/// is null or empty.
///
///
internal ParameterBindingParameterDefaultValueException(
Exception innerException,
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(
innerException,
errorCategory,
invocationInfo,
errorPosition,
parameterName,
parameterType,
typeSpecified,
resourceString,
errorId,
args)
{
}
#endregion Preferred constructors
#region serialization
///
/// Constructs a ParameterBindingParameterDefaultValueException using serialized data
///
///
///
/// serialization information
///
///
///
/// streaming context
///
protected ParameterBindingParameterDefaultValueException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
}
#endregion serialization
}
} // namespace System.Management.Automation