File size: 6,489 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Reflection;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace System.Management.Automation.Runspaces
{
/// <summary>
/// Defines exception thrown when a PSSnapin was not able to load into current runspace.
/// </summary>
/// <!--
/// Implementation of PSSnapInException requires it to
/// 1. Implement IContainsErrorRecord,
/// 2. ISerializable
///
/// Basic information for this exception includes,
/// 1. PSSnapin name
/// 2. Inner exception.
/// -->
public class PSSnapInException : RuntimeException
{
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
/// <param name="PSSnapin">PSSnapin for the exception.</param>
/// <param name="message">Message with load failure detail.</param>
internal PSSnapInException(string PSSnapin, string message)
: base()
{
_PSSnapin = PSSnapin;
_reason = message;
CreateErrorRecord();
}
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
/// <param name="PSSnapin">PSSnapin for the exception.</param>
/// <param name="message">Message with load failure detail.</param>
/// <param name="warning">Whether this is just a warning for PSSnapin load.</param>
internal PSSnapInException(string PSSnapin, string message, bool warning)
: base()
{
_PSSnapin = PSSnapin;
_reason = message;
_warning = warning;
CreateErrorRecord();
}
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
/// <param name="PSSnapin">PSSnapin for the exception.</param>
/// <param name="message">Message with load failure detail.</param>
/// <param name="exception">Exception for PSSnapin load failure.</param>
internal PSSnapInException(string PSSnapin, string message, Exception exception)
: base(message, exception)
{
_PSSnapin = PSSnapin;
_reason = message;
CreateErrorRecord();
}
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
public PSSnapInException() : base()
{
}
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
/// <param name="message">Error message.</param>
public PSSnapInException(string message)
: base(message)
{
}
/// <summary>
/// Initiate an instance of PSSnapInException.
/// </summary>
/// <param name="message">Error message.</param>
/// <param name="innerException">Inner exception.</param>
public PSSnapInException(string message, Exception innerException)
: base(message, innerException)
{
}
/// <summary>
/// Create the internal error record.
/// The ErrorRecord created will be stored in the _errorRecord member.
/// </summary>
private void CreateErrorRecord()
{
// if _PSSnapin or _reason 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(_PSSnapin) && !string.IsNullOrEmpty(_reason))
{
Assembly currentAssembly = typeof(PSSnapInException).Assembly;
if (_warning)
{
_errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "PSSnapInLoadWarning", ErrorCategory.ResourceUnavailable, null);
_errorRecord.ErrorDetails = new ErrorDetails(string.Format(ConsoleInfoErrorStrings.PSSnapInLoadWarning, _PSSnapin, _reason));
}
else
{
_errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(this), "PSSnapInLoadFailure", ErrorCategory.ResourceUnavailable, null);
_errorRecord.ErrorDetails = new ErrorDetails(string.Format(ConsoleInfoErrorStrings.PSSnapInLoadFailure, _PSSnapin, _reason));
}
}
}
private readonly bool _warning = false;
private ErrorRecord _errorRecord;
private bool _isErrorRecordOriginallyNull;
/// <summary>
/// Gets error record embedded in this exception.
/// </summary>
/// <!--
/// This property is required as part of IErrorRecordContainer
/// interface.
/// -->
public override ErrorRecord ErrorRecord
{
get
{
if (_errorRecord == null)
{
_isErrorRecordOriginallyNull = true;
_errorRecord = new ErrorRecord(
new ParentContainsErrorRecordException(this),
"PSSnapInException",
ErrorCategory.NotSpecified,
null);
}
return _errorRecord;
}
}
private readonly string _PSSnapin = string.Empty;
private readonly string _reason = string.Empty;
/// <summary>
/// Gets message for this exception.
/// </summary>
public override string Message
{
get
{
if (_errorRecord != null && !_isErrorRecordOriginallyNull)
{
return _errorRecord.ToString();
}
return base.Message;
}
}
#region Serialization
/// <summary>
/// Initiate a PSSnapInException instance.
/// </summary>
/// <param name="info">Serialization information.</param>
/// <param name="context">Streaming context.</param>
[Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]
protected PSSnapInException(SerializationInfo info,
StreamingContext context)
{
throw new NotSupportedException();
}
#endregion Serialization
}
}
|