/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Runtime.Serialization; using System.Reflection; #if CORECLR // Use stubs for SystemException, SerializationInfo and SecurityPermissionAttribute using Microsoft.PowerShell.CoreClr.Stubs; #else using System.Security.Permissions; #endif namespace System.Management.Automation.Runspaces { /// /// Defines exception thrown when runspace configuration attribute is not defined correctly. /// /// [Serializable] public class RunspaceConfigurationAttributeException : SystemException, IContainsErrorRecord { /// /// Initiate an instance of RunspaceConfigurationAttributeException. /// /// Error detail for the exception /// Assembly on which runspace configuration attribute is defined or should be defined. internal RunspaceConfigurationAttributeException(string error, string assemblyName) : base() { _error = error; _assemblyName = assemblyName; CreateErrorRecord(); } /// /// Initiate an instance of RunspaceConfigurationAttributeException. /// public RunspaceConfigurationAttributeException() : base() { } /// /// Initiate an instance of RunspaceConfigurationAttributeException. /// /// Error message public RunspaceConfigurationAttributeException(string message) : base(message) { } /// /// Initiate an instance of RunspaceConfigurationAttributeException. /// /// Error message /// Inner exception public RunspaceConfigurationAttributeException(string message, Exception innerException) : base(message, innerException) { } /// /// Initiate an instance of RunspaceConfigurationAttributeException. /// /// Error detail /// Assembly on which runspace configuration attribute is defined or should be defined. /// The inner exception of this exception internal RunspaceConfigurationAttributeException(string error, string assemblyName, Exception innerException) : base(innerException.Message, innerException) { _error = error; _assemblyName = assemblyName; CreateErrorRecord(); } /// /// Create the internal error record based on helpTopic. /// The ErrorRecord created will be stored in the _errorRecord member. /// private void CreateErrorRecord() { // if _error is empty, this exception is created using default // constructor. Don't create the error record since there is // no useful information anyway. if (!String.IsNullOrEmpty(_error) && !String.IsNullOrEmpty(_assemblyName)) { _errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), _error, ErrorCategory.ResourceUnavailable, null); _errorRecord.ErrorDetails = new ErrorDetails(typeof(RunspaceConfigurationAttributeException).GetTypeInfo().Assembly, "MiniShellErrors", _error, _assemblyName); } } private ErrorRecord _errorRecord; /// /// Gets error record embedded in this exception. /// /// public ErrorRecord ErrorRecord { get { return _errorRecord; } } private string _error = ""; /// /// Get localized error message. /// /// error public string Error { get { return _error; } } private string _assemblyName = ""; /// /// Gets assembly name on which runspace configuration attribute is defined or should be defined. /// /// error public string AssemblyName { get { return _assemblyName; } } /// /// Gets message for this exception. /// public override string Message { get { if (_errorRecord != null) { return _errorRecord.ToString(); } return base.Message; } } #region Serialization /// /// Initiate a RunspaceConfigurationAttributeException instance. /// /// Serialization information /// Streaming context protected RunspaceConfigurationAttributeException(SerializationInfo info, StreamingContext context) : base(info, context) { _error = info.GetString("Error"); _assemblyName = info.GetString("AssemblyName"); CreateErrorRecord(); } /// /// Get object data from serizliation information. /// /// Serialization information /// Streaming context [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw PSTraceSource.NewArgumentNullException("info"); } base.GetObjectData(info, context); info.AddValue("Error", _error); info.AddValue("AssemblyName", _assemblyName); } #endregion Serialization } }