internal void ReportJobFailure(IContainsErrorRecord exception) { TerminatingErrorTracker terminatingErrorTracker = TerminatingErrorTracker.GetTracker(this.JobContext.CmdletInvocationInfo); bool sessionWasAlreadyTerminated = false; bool isThisTerminatingError = false; Exception brokenSessionException = null; lock (_jobStateLock) { if (!_jobWasStopped) { brokenSessionException = terminatingErrorTracker.GetExceptionIfBrokenSession( this.JobContext.Session, this.JobContext.CmdletInvocationContext.CmdletDefinitionContext.SkipTestConnection, out sessionWasAlreadyTerminated); } } if (brokenSessionException != null) { string brokenSessionMessage = string.Format( CultureInfo.InvariantCulture, CmdletizationResources.CimJob_BrokenSession, brokenSessionException.Message); exception = CimJobException.CreateWithFullControl( this.JobContext, brokenSessionMessage, "CimJob_BrokenCimSession", ErrorCategory.ResourceUnavailable, brokenSessionException); isThisTerminatingError = true; } else { CimJobException cje = exception as CimJobException; if ((cje != null) && (cje.IsTerminatingError)) { terminatingErrorTracker.MarkSessionAsTerminated(this.JobContext.Session, out sessionWasAlreadyTerminated); isThisTerminatingError = true; } } bool writeError = !sessionWasAlreadyTerminated; if (writeError) { lock (_jobStateLock) { if (_jobWasStopped) { writeError = false; } } } ErrorRecord errorRecord = exception.ErrorRecord; errorRecord.SetInvocationInfo(this.JobContext.CmdletInvocationInfo); errorRecord.PreserveInvocationInfoOnce = true; if (writeError) { lock (_jobStateLock) { if (!_alreadyReachedCompletedState) { if (isThisTerminatingError) { this.Error.Add(errorRecord); CmdletMethodInvoker <bool> methodInvoker = terminatingErrorTracker.GetErrorReportingDelegate(errorRecord); this.Results.Add(new PSStreamObject(PSStreamObjectType.ShouldMethod, methodInvoker)); } else { this.WriteError(errorRecord); } } } } this.SetCompletedJobState(JobState.Failed, errorRecord.Exception); }
private string GetFunctionExceptionMessage(IContainsErrorRecord exception) { return($"EXCEPTION: {_errorRecordFormatter.Format(exception.ErrorRecord)}"); }