/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Runtime.Serialization;
using System.Text;
using System.Collections.ObjectModel;
#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 a PSSnapin was not able to load into current runspace.
///
///
[Serializable]
public class PSConsoleLoadException : SystemException, IContainsErrorRecord
{
///
/// Intiate an instance of PSConsoleLoadException.
///
/// Console info object for the exception
/// A collection of PSSnapInExceptions.
internal PSConsoleLoadException(MshConsoleInfo consoleInfo, Collection exceptions)
: base()
{
if (!String.IsNullOrEmpty(consoleInfo.Filename))
_consoleFileName = consoleInfo.Filename;
if (exceptions != null)
{
_PSSnapInExceptions = exceptions;
}
CreateErrorRecord();
}
///
/// Initiate an instance of PSConsoleLoadException.
///
public PSConsoleLoadException() : base()
{
}
///
/// Initiate an instance of PSConsoleLoadException.
///
/// Error message
public PSConsoleLoadException(string message)
: base(message)
{
}
///
/// Initiate an instance of PSConsoleLoadException.
///
/// Error message
/// Inner exception
public PSConsoleLoadException(string message, Exception innerException)
: base(message, innerException)
{
}
///
/// Create the internal error record.
/// The ErrorRecord created will be stored in the _errorRecord member.
///
private void CreateErrorRecord()
{
StringBuilder sb = new StringBuilder();
if (PSSnapInExceptions != null)
{
foreach (PSSnapInException e in PSSnapInExceptions)
{
sb.Append("\n");
sb.Append(e.Message);
}
}
_errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "ConsoleLoadFailure", ErrorCategory.ResourceUnavailable, null);
_errorRecord.ErrorDetails = new ErrorDetails(String.Format(ConsoleInfoErrorStrings.ConsoleLoadFailure, _consoleFileName, sb.ToString()));
}
private ErrorRecord _errorRecord;
///
/// Gets error record embedded in this exception.
///
///
public ErrorRecord ErrorRecord
{
get
{
return _errorRecord;
}
}
private string _consoleFileName = "";
private Collection _PSSnapInExceptions = new Collection();
internal Collection PSSnapInExceptions
{
get
{
return _PSSnapInExceptions;
}
}
///
/// Gets message for this exception.
///
public override string Message
{
get
{
if (_errorRecord != null)
{
return _errorRecord.ToString();
}
else
{
return base.Message;
}
}
}
#region Serialization
///
/// Initiate a PSConsoleLoadException instance.
///
/// Serialization information
/// Streaming context
protected PSConsoleLoadException(SerializationInfo info,
StreamingContext context)
: base(info, context)
{
_consoleFileName = info.GetString("ConsoleFileName");
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("ConsoleFileName", _consoleFileName);
}
#endregion Serialization
}
}