File size: 14,955 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Diagnostics;
using System.Globalization;
using System.Management.Automation;
using Microsoft.Management.Infrastructure.Options;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
#region class ErrorToErrorRecord
/// <summary>
/// <para>
/// Convert error or exception to <see cref="System.Management.Automation.ErrorRecord"/>
/// </para>
/// </summary>
internal sealed class ErrorToErrorRecord
{
/// <summary>
/// <para>
/// Convert ErrorRecord from exception object, <see cref="Exception"/>
/// can be either <see cref="CimException"/> or general <see cref="Exception"/>.
/// </para>
/// </summary>
/// <param name="inner"></param>
/// <param name="context">The context starting the operation, which generated the error.</param>
/// <param name="cimResultContext">The CimResultContext used to provide ErrorSource, etc. info.</param>
/// <returns></returns>
internal static ErrorRecord ErrorRecordFromAnyException(
InvocationContext context,
Exception inner,
CimResultContext cimResultContext)
{
Debug.Assert(inner != null, "Caller should verify inner != null");
if (inner is CimException cimException)
{
return CreateFromCimException(context, cimException, cimResultContext);
}
if (inner is IContainsErrorRecord containsErrorRecord)
{
return InitializeErrorRecord(context,
exception: inner,
errorId: "CimCmdlet_" + containsErrorRecord.ErrorRecord.FullyQualifiedErrorId,
errorCategory: containsErrorRecord.ErrorRecord.CategoryInfo.Category,
cimResultContext: cimResultContext);
}
else
{
return InitializeErrorRecord(context,
exception: inner,
errorId: "CimCmdlet_" + inner.GetType().Name,
errorCategory: ErrorCategory.NotSpecified,
cimResultContext: cimResultContext);
}
}
#region Helper functions
/// <summary>
/// Create <see cref="ErrorRecord"/> from <see cref="CimException"/> object.
/// </summary>
/// <param name="context"></param>
/// <param name="cimException"></param>
/// <param name="cimResultContext">The CimResultContext used to provide ErrorSource, etc. info.</param>
/// <returns></returns>
internal static ErrorRecord CreateFromCimException(
InvocationContext context,
CimException cimException,
CimResultContext cimResultContext)
{
Debug.Assert(cimException != null, "Caller should verify cimException != null");
return InitializeErrorRecord(context, cimException, cimResultContext);
}
/// <summary>
/// Create <see cref="ErrorRecord"/> from <see cref="Exception"/> object.
/// </summary>
/// <param name="context"></param>
/// <param name="exception"></param>
/// <param name="errorId"></param>
/// <param name="errorCategory"></param>
/// <param name="cimResultContext">The CimResultContext used to provide ErrorSource, etc. info.</param>
/// <returns></returns>
internal static ErrorRecord InitializeErrorRecord(
InvocationContext context,
Exception exception,
string errorId,
ErrorCategory errorCategory,
CimResultContext cimResultContext)
{
return InitializeErrorRecordCore(
context,
exception: exception,
errorId: errorId,
errorCategory: errorCategory,
cimResultContext: cimResultContext);
}
/// <summary>
/// Create <see cref="ErrorRecord"/> from <see cref="CimException"/> object.
/// </summary>
/// <param name="context"></param>
/// <param name="cimException"></param>
/// <param name="cimResultContext">The CimResultContext used to provide ErrorSource, etc. info.</param>
/// <returns></returns>
internal static ErrorRecord InitializeErrorRecord(
InvocationContext context,
CimException cimException,
CimResultContext cimResultContext)
{
ErrorRecord errorRecord = InitializeErrorRecordCore(
context,
exception: cimException,
errorId: cimException.MessageId ?? "MiClientApiError_" + cimException.NativeErrorCode,
errorCategory: ConvertCimExceptionToErrorCategory(cimException),
cimResultContext: cimResultContext);
if (cimException.ErrorData != null)
{
errorRecord.CategoryInfo.TargetName = cimException.ErrorSource;
}
return errorRecord;
}
/// <summary>
/// Create <see cref="ErrorRecord"/> from <see cref="Exception"/> object.
/// </summary>
/// <param name="context"></param>
/// <param name="exception"></param>
/// <param name="errorId"></param>
/// <param name="errorCategory"></param>
/// <param name="cimResultContext">The CimResultContext used to provide ErrorSource, etc. info.</param>
/// <returns></returns>
internal static ErrorRecord InitializeErrorRecordCore(
InvocationContext context,
Exception exception,
string errorId,
ErrorCategory errorCategory,
CimResultContext cimResultContext)
{
object theTargetObject = null;
if (cimResultContext != null)
{
theTargetObject = cimResultContext.ErrorSource;
}
if (theTargetObject == null)
{
if (context != null)
{
if (context.TargetCimInstance != null)
{
theTargetObject = context.TargetCimInstance;
}
}
}
ErrorRecord coreErrorRecord = new(
exception: exception,
errorId: errorId,
errorCategory: errorCategory,
targetObject: theTargetObject);
if (context == null)
{
return coreErrorRecord;
}
System.Management.Automation.Remoting.OriginInfo originInfo = new(
context.ComputerName,
Guid.Empty);
ErrorRecord errorRecord = new System.Management.Automation.Runspaces.RemotingErrorRecord(
coreErrorRecord,
originInfo);
DebugHelper.WriteLogEx("Created RemotingErrorRecord.", 0);
return errorRecord;
}
/// <summary>
/// Convert <see cref="CimException"/> to <see cref="ErrorCategory"/>.
/// </summary>
/// <param name="cimException"></param>
/// <returns></returns>
internal static ErrorCategory ConvertCimExceptionToErrorCategory(CimException cimException)
{
ErrorCategory result = ErrorCategory.NotSpecified;
if (cimException.ErrorData != null)
{
result = ConvertCimErrorToErrorCategory(cimException.ErrorData);
}
if (result == ErrorCategory.NotSpecified)
{
result = ConvertCimNativeErrorCodeToErrorCategory(cimException.NativeErrorCode);
}
return result;
}
/// <summary>
/// Convert <see cref="NativeErrorCode"/> to <see cref="ErrorCategory"/>.
/// </summary>
/// <param name="nativeErrorCode"></param>
/// <returns></returns>
internal static ErrorCategory ConvertCimNativeErrorCodeToErrorCategory(NativeErrorCode nativeErrorCode)
{
switch (nativeErrorCode)
{
case NativeErrorCode.Failed:
return ErrorCategory.NotSpecified;
case NativeErrorCode.AccessDenied:
return ErrorCategory.PermissionDenied;
case NativeErrorCode.InvalidNamespace:
return ErrorCategory.MetadataError;
case NativeErrorCode.InvalidParameter:
return ErrorCategory.InvalidArgument;
case NativeErrorCode.InvalidClass:
return ErrorCategory.MetadataError;
case NativeErrorCode.NotFound:
return ErrorCategory.ObjectNotFound;
case NativeErrorCode.NotSupported:
return ErrorCategory.NotImplemented;
case NativeErrorCode.ClassHasChildren:
return ErrorCategory.MetadataError;
case NativeErrorCode.ClassHasInstances:
return ErrorCategory.MetadataError;
case NativeErrorCode.InvalidSuperClass:
return ErrorCategory.MetadataError;
case NativeErrorCode.AlreadyExists:
return ErrorCategory.ResourceExists;
case NativeErrorCode.NoSuchProperty:
return ErrorCategory.MetadataError;
case NativeErrorCode.TypeMismatch:
return ErrorCategory.InvalidType;
case NativeErrorCode.QueryLanguageNotSupported:
return ErrorCategory.NotImplemented;
case NativeErrorCode.InvalidQuery:
return ErrorCategory.InvalidArgument;
case NativeErrorCode.MethodNotAvailable:
return ErrorCategory.MetadataError;
case NativeErrorCode.MethodNotFound:
return ErrorCategory.MetadataError;
case NativeErrorCode.NamespaceNotEmpty:
return ErrorCategory.MetadataError;
case NativeErrorCode.InvalidEnumerationContext:
return ErrorCategory.MetadataError;
case NativeErrorCode.InvalidOperationTimeout:
return ErrorCategory.InvalidArgument;
case NativeErrorCode.PullHasBeenAbandoned:
return ErrorCategory.OperationStopped;
case NativeErrorCode.PullCannotBeAbandoned:
return ErrorCategory.CloseError;
case NativeErrorCode.FilteredEnumerationNotSupported:
return ErrorCategory.NotImplemented;
case NativeErrorCode.ContinuationOnErrorNotSupported:
return ErrorCategory.NotImplemented;
case NativeErrorCode.ServerLimitsExceeded:
return ErrorCategory.ResourceBusy;
case NativeErrorCode.ServerIsShuttingDown:
return ErrorCategory.ResourceUnavailable;
default:
return ErrorCategory.NotSpecified;
}
}
/// <summary>
/// Convert <see cref="cimError"/> to <see cref="ErrorCategory"/>.
/// </summary>
/// <param name="cimError"></param>
/// <returns></returns>
internal static ErrorCategory ConvertCimErrorToErrorCategory(CimInstance cimError)
{
if (cimError == null)
{
return ErrorCategory.NotSpecified;
}
CimProperty errorCategoryProperty = cimError.CimInstanceProperties[@"Error_Category"];
if (errorCategoryProperty == null)
{
return ErrorCategory.NotSpecified;
}
ErrorCategory errorCategoryValue;
if (!LanguagePrimitives.TryConvertTo<ErrorCategory>(errorCategoryProperty.Value, CultureInfo.InvariantCulture, out errorCategoryValue))
{
return ErrorCategory.NotSpecified;
}
return errorCategoryValue;
}
#endregion
}
#endregion
/// <summary>
/// <para>
/// Write error to pipeline
/// </para>
/// </summary>
internal sealed class CimWriteError : CimSyncAction
{
/// <summary>
/// Initializes a new instance of the <see cref="CimWriteError"/> class
/// with the specified <see cref="CimInstance"/>.
/// </summary>
/// <param name="error"></param>
public CimWriteError(CimInstance error, InvocationContext context)
{
this.Error = error;
this.CimInvocationContext = context;
}
/// <summary>
/// Initializes a new instance of the <see cref="CimWriteError"/> class
/// with the specified <see cref="Exception"/>.
/// </summary>
/// <param name="exception"></param>
public CimWriteError(Exception exception, InvocationContext context, CimResultContext cimResultContext)
{
this.Exception = exception;
this.CimInvocationContext = context;
this.ResultContext = cimResultContext;
}
/// <summary>
/// <para>
/// Write error to pipeline
/// </para>
/// </summary>
/// <param name="cmdlet"></param>
public override void Execute(CmdletOperationBase cmdlet)
{
Debug.Assert(cmdlet != null, "Caller should verify that cmdlet != null");
try
{
Exception errorException = (Error != null) ? new CimException(Error) : this.Exception;
// PS engine takes care of handling error action
cmdlet.WriteError(ErrorToErrorRecord.ErrorRecordFromAnyException(this.CimInvocationContext, errorException, this.ResultContext));
// if user wants to continue, we will get here
this.responseType = CimResponseType.Yes;
}
catch
{
this.responseType = CimResponseType.NoToAll;
throw;
}
finally
{
// unblocking the waiting thread
this.OnComplete();
}
}
#region members
/// <summary>
/// <para>
/// Error instance
/// </para>
/// </summary>
internal CimInstance Error { get; }
/// <summary>
/// <para>
/// Exception object
/// </para>
/// </summary>
internal Exception Exception { get; }
internal InvocationContext CimInvocationContext { get; }
internal CimResultContext ResultContext { get; }
#endregion
}
}
|