// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Management.Automation.Internal; using System.Management.Automation.Language; using System.Runtime.Serialization; namespace System.Management.Automation { /// /// The exception thrown if the specified value can not be bound parameter of a command. /// 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(nameof(resourceString)); } if (string.IsNullOrEmpty(errorId)) { throw PSTraceSource.NewArgumentException(nameof(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(nameof(invocationInfo)); } if (string.IsNullOrEmpty(resourceString)) { throw PSTraceSource.NewArgumentException(nameof(resourceString)); } if (string.IsNullOrEmpty(errorId)) { throw PSTraceSource.NewArgumentException(nameof(errorId)); } _invocationInfo = invocationInfo; _commandName = invocationInfo.MyCommand.Name; _parameterName = parameterName; _parameterType = parameterType; _typeSpecified = typeSpecified; 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(nameof(pbex)); } if (string.IsNullOrEmpty(resourceString)) { throw PSTraceSource.NewArgumentException(nameof(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 /// [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] protected ParameterBindingException( SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); } #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 ??= 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 readonly string _parameterName = string.Empty; /// /// Gets the type the parameter is expecting. /// public Type ParameterType { get { return _parameterType; } } private readonly Type _parameterType; /// /// Gets the Type that was specified as the parameter value. /// public Type TypeSpecified { get { return _typeSpecified; } } private readonly Type _typeSpecified; /// /// Gets the errorId of this ParameterBindingException. /// public string ErrorId { get { return _errorId; } } private readonly string _errorId; /// /// Gets the line in the script at which the error occurred. /// public Int64 Line { get { return _line; } } private readonly 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 readonly Int64 _offset = Int64.MinValue; /// /// Gets the invocation information about the command. /// public InvocationInfo CommandInvocation { get { return _invocationInfo; } } private readonly InvocationInfo _invocationInfo; #endregion Properties #region private private readonly string _resourceString; private readonly object[] _args = Array.Empty(); private readonly string _commandName; private string BuildMessage() { object[] messageArgs = Array.Empty(); 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 } 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) { if (innerException is ValidationMetadataException validationException && validationException.SwallowException) { _swallowException = true; } } #endregion Preferred constructors #region serialization /// /// Constructs a ParameterBindingValidationException from serialized data. /// /// /// serialization information /// /// /// streaming context /// [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] protected ParameterBindingValidationException( SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); } #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 } 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 /// [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] protected ParameterBindingArgumentTransformationException( SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); } #endregion serialization } 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 /// [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] protected ParameterBindingParameterDefaultValueException( SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); } #endregion serialization } }