internal void WriteDebugLine(string message, ref ActionPreference preference)
        {
            ErrorRecord error = null;

            switch (preference)
            {
            case ActionPreference.SilentlyContinue:
            case ActionPreference.Ignore:
                return;

            case ActionPreference.Stop:
            {
                this.WriteDebugLineHelper(message);
                error = new ErrorRecord(new ParentContainsErrorRecordException(InternalHostUserInterfaceStrings.WriteDebugLineStoppedError), "ActionPreferenceStop", ErrorCategory.OperationStopped, null);
                ActionPreferenceStopException exception2 = new ActionPreferenceStopException(error);
                throw exception2;
            }

            case ActionPreference.Continue:
                this.WriteDebugLineHelper(message);
                return;

            case ActionPreference.Inquire:
                if (!this.DebugShouldContinue(message, ref preference))
                {
                    error = new ErrorRecord(new ParentContainsErrorRecordException(InternalHostUserInterfaceStrings.WriteDebugLineStoppedError), "UserStopRequest", ErrorCategory.OperationStopped, null);
                    ActionPreferenceStopException exception = new ActionPreferenceStopException(error);
                    throw exception;
                }
                this.WriteDebugLineHelper(message);
                return;
            }
            throw PSTraceSource.NewArgumentException("preference", "InternalHostUserInterfaceStrings", "UnsupportedPreferenceError", new object[] { (ActionPreference)preference });
        }
Beispiel #2
0
        internal void WriteDebugLine(string message, ref ActionPreference preference)
        {
            using (InternalHostUserInterface.tracer.TraceMethod(message, new object[0]))
            {
                InternalHostUserInterface.tracer.WriteLine((object)preference);
                switch (preference)
                {
                case ActionPreference.SilentlyContinue:
                    break;

                case ActionPreference.Stop:
                    this.WriteDebugLineHelper(message);
                    ActionPreferenceStopException preferenceStopException1 = new ActionPreferenceStopException(new ErrorRecord((Exception) new ParentContainsErrorRecordException(ResourceManagerCache.GetResourceString("InternalHostUserInterfaceStrings", "WriteDebugLineStoppedError")), "ActionPreferenceStop", ErrorCategory.OperationStopped, (object)null));
                    InternalHostUserInterface.tracer.TraceException((Exception)preferenceStopException1);
                    throw preferenceStopException1;

                case ActionPreference.Continue:
                    this.WriteDebugLineHelper(message);
                    break;

                case ActionPreference.Inquire:
                    if (!this.DebugShouldContinue(message, ref preference))
                    {
                        ActionPreferenceStopException preferenceStopException2 = new ActionPreferenceStopException(new ErrorRecord((Exception) new ParentContainsErrorRecordException(ResourceManagerCache.GetResourceString("InternalHostUserInterfaceStrings", "WriteDebugLineStoppedError")), "UserStopRequest", ErrorCategory.OperationStopped, (object)null));
                        InternalHostUserInterface.tracer.TraceException((Exception)preferenceStopException2);
                        throw preferenceStopException2;
                    }
                    this.WriteDebugLineHelper(message);
                    break;

                default:
                    throw InternalHostUserInterface.tracer.NewArgumentException(nameof(preference), "InternalHostUserInterfaceStrings", "UnsupportedPreferenceError", (object)preference);
                }
            }
        }
        WriteDebugLine(string message, ref ActionPreference preference)
        {
            string      errorMsg    = null;
            ErrorRecord errorRecord = null;

            switch (preference)
            {
            case ActionPreference.Continue:
            case ActionPreference.Break:
                WriteDebugLineHelper(message);
                break;

            case ActionPreference.SilentlyContinue:
            case ActionPreference.Ignore:
                break;

            case ActionPreference.Inquire:
                if (!DebugShouldContinue(message, ref preference))
                {
                    // user asked to exit with an error

                    errorMsg    = InternalHostUserInterfaceStrings.WriteDebugLineStoppedError;
                    errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(errorMsg),
                                                  "UserStopRequest", ErrorCategory.OperationStopped, null);
                    ActionPreferenceStopException e = new ActionPreferenceStopException(errorRecord);
                    // We cannot call ThrowTerminatingError since this is not a cmdlet or provider
                    throw e;
                }
                else
                {
                    WriteDebugLineHelper(message);
                }

                break;

            case ActionPreference.Stop:
                WriteDebugLineHelper(message);

                errorMsg    = InternalHostUserInterfaceStrings.WriteDebugLineStoppedError;
                errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(errorMsg),
                                              "ActionPreferenceStop", ErrorCategory.OperationStopped, null);
                ActionPreferenceStopException ense = new ActionPreferenceStopException(errorRecord);
                // We cannot call ThrowTerminatingError since this is not a cmdlet or provider
                throw ense;

            default:
                Dbg.Assert(false, "all preferences should be checked");
                throw PSTraceSource.NewArgumentException(nameof(preference),
                                                         InternalHostUserInterfaceStrings.UnsupportedPreferenceError, preference);
                // break;
            }
        }
Beispiel #4
0
        public async Task TestInvokeAsync_ThrowsPowerShellErrorOnErrorActionStop()
        {
            const string expectedValue = "Test Exception";

            using (PowerShell powerShell = PowerShell.Create())
            {
                powerShell.AddVariable("varName", expectedValue);
                powerShell.AddScript("$ErrorActionPreference = \"Stop\"");
                powerShell.AddScript("Write-Error $varName");

                ActionPreferenceStopException e =
                    await Assert.ThrowsExceptionAsync <ActionPreferenceStopException>(() => powerShell.InvokeAsync());

                Assert.AreEqual(expectedValue, e.ErrorRecord.Exception.Message);
            }
        }
        WriteDebugLine(string message, ref ActionPreference preference)
        {
            string errorMsg = null;
            ErrorRecord errorRecord = null;
            switch (preference)
            {
                case ActionPreference.Continue:
                    WriteDebugLineHelper(message);
                    break;
                case ActionPreference.SilentlyContinue:
                case ActionPreference.Ignore:
                    break;
                case ActionPreference.Inquire:
                    if (!DebugShouldContinue(message, ref preference))
                    {
                        // user asked to exit with an error

                        errorMsg = InternalHostUserInterfaceStrings.WriteDebugLineStoppedError;
                        errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(errorMsg),
                            "UserStopRequest", ErrorCategory.OperationStopped, null);
                        ActionPreferenceStopException e = new ActionPreferenceStopException(errorRecord);
                        // We cannot call ThrowTerminatingError since this is not a cmdlet or provider
                        throw e;
                    }
                    else
                    {
                        WriteDebugLineHelper(message);
                    }
                    break;
                case ActionPreference.Stop:
                    WriteDebugLineHelper(message);

                    errorMsg = InternalHostUserInterfaceStrings.WriteDebugLineStoppedError;
                    errorRecord = new ErrorRecord(new ParentContainsErrorRecordException(errorMsg),
                        "ActionPreferenceStop", ErrorCategory.OperationStopped, null);
                    ActionPreferenceStopException ense = new ActionPreferenceStopException(errorRecord);
                    // We cannot call ThrowTerminatingError since this is not a cmdlet or provider
                    throw ense;
                default:
                    Dbg.Assert(false, "all preferences should be checked");
                    throw PSTraceSource.NewArgumentException("preference",
                        InternalHostUserInterfaceStrings.UnsupportedPreferenceError, preference);
                    // break;
            }
        }