Exemplo n.º 1
0
        internal static void SetErrorVariables(IScriptExtent extent, RuntimeException rte, ExecutionContext context, Pipe outputPipe)
        {
            string    newValue       = null;
            Exception innerException = rte;
            int       num            = 0;

            while ((innerException != null) && (num++ < 10))
            {
                if (!string.IsNullOrEmpty(innerException.StackTrace))
                {
                    newValue = innerException.StackTrace;
                }
                innerException = innerException.InnerException;
            }
            context.SetVariable(SpecialVariables.StackTraceVarPath, newValue);
            InterpreterError.UpdateExceptionErrorRecordPosition(rte, extent);
            ErrorRecord record = rte.ErrorRecord.WrapException(rte);

            if (!(rte is PipelineStoppedException))
            {
                if (outputPipe != null)
                {
                    outputPipe.AppendVariableList(VariableStreamKind.Error, record);
                }
                context.AppendDollarError(record);
            }
        }
Exemplo n.º 2
0
        internal static RuntimeException ConvertToRuntimeException(Exception exception, IScriptExtent extent)
        {
            RuntimeException exception2 = exception as RuntimeException;

            if (exception2 == null)
            {
                IContainsErrorRecord record      = exception as IContainsErrorRecord;
                ErrorRecord          errorRecord = (record != null) ? record.ErrorRecord : new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.OperationStopped, null);
                exception2 = new RuntimeException(exception.Message, exception, errorRecord);
            }
            InterpreterError.UpdateExceptionErrorRecordPosition(exception2, extent);
            return(exception2);
        }
Exemplo n.º 3
0
        internal static RuntimeException ConvertToException(object result, IScriptExtent extent)
        {
            result = PSObject.Base(result);
            RuntimeException exception = result as RuntimeException;

            if (exception != null)
            {
                InterpreterError.UpdateExceptionErrorRecordPosition(exception, extent);
                exception.WasThrownFromThrowStatement = true;
                return(exception);
            }
            ErrorRecord errorRecord = result as ErrorRecord;

            if (errorRecord != null)
            {
                exception = new RuntimeException(errorRecord.ToString(), errorRecord.Exception, errorRecord)
                {
                    WasThrownFromThrowStatement = true
                };
                InterpreterError.UpdateExceptionErrorRecordPosition(exception, extent);
                return(exception);
            }
            Exception exception3 = result as Exception;

            if (exception3 != null)
            {
                errorRecord = new ErrorRecord(exception3, exception3.Message, ErrorCategory.OperationStopped, null);
                exception   = new RuntimeException(exception3.Message, exception3, errorRecord)
                {
                    WasThrownFromThrowStatement = true
                };
                InterpreterError.UpdateExceptionErrorRecordPosition(exception, extent);
                return(exception);
            }
            string message = LanguagePrimitives.IsNull(result) ? "ScriptHalted" : ParserOps.ConvertTo <string>(result, PositionUtilities.EmptyExtent);

            exception3  = new RuntimeException(message, null);
            errorRecord = new ErrorRecord(exception3, message, ErrorCategory.OperationStopped, null);
            exception   = new RuntimeException(message, exception3, errorRecord)
            {
                WasThrownFromThrowStatement = true
            };
            exception.SetTargetObject(result);
            InterpreterError.UpdateExceptionErrorRecordPosition(exception, extent);
            return(exception);
        }
Exemplo n.º 4
0
 internal static void DefineFunction(ExecutionContext context, FunctionDefinitionAst functionDefinitionAst, ScriptBlockExpressionWrapper scriptBlockExpressionWrapper)
 {
     try
     {
         ScriptBlock scriptBlock = scriptBlockExpressionWrapper.GetScriptBlock(context, functionDefinitionAst.IsFilter);
         context.EngineSessionState.SetFunctionRaw(functionDefinitionAst.Name, scriptBlock, context.EngineSessionState.CurrentScope.ScopeOrigin);
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         RuntimeException exception2 = exception as RuntimeException;
         if (exception2 == null)
         {
             throw ExceptionHandlingOps.ConvertToRuntimeException(exception, functionDefinitionAst.Extent);
         }
         InterpreterError.UpdateExceptionErrorRecordPosition(exception2, functionDefinitionAst.Extent);
         throw;
     }
 }
Exemplo n.º 5
0
 internal static void DefineWorkflows(ExecutionContext context, ScriptBlockAst scriptBlockAst)
 {
     try
     {
         foreach (WorkflowInfo info in Utils.GetAstToWorkflowConverterAndEnsureWorkflowModuleLoaded(context).CompileWorkflows(scriptBlockAst, null))
         {
             context.EngineSessionState.SetWorkflowRaw(info, context.EngineSessionState.CurrentScope.ScopeOrigin);
         }
     }
     catch (Exception exception)
     {
         CommandProcessorBase.CheckForSevereException(exception);
         RuntimeException exception2 = exception as RuntimeException;
         if (exception2 == null)
         {
             throw ExceptionHandlingOps.ConvertToRuntimeException(exception, scriptBlockAst.Extent);
         }
         InterpreterError.UpdateExceptionErrorRecordPosition(exception2, scriptBlockAst.Extent);
         throw;
     }
 }
Exemplo n.º 6
0
 internal static void ThrowError(ScriptBlockToPowerShellNotSupportedException ex, Ast ast)
 {
     InterpreterError.UpdateExceptionErrorRecordPosition(ex, ast.Extent);
     throw ex;
 }
Exemplo n.º 7
0
        internal static void CheckActionPreference(FunctionContext funcContext, Exception exception)
        {
            ActionPreference preference;

            if (exception is TargetInvocationException)
            {
                exception = exception.InnerException;
            }
            CommandProcessorBase.CheckForSevereException(exception);
            RuntimeException exception2 = exception as RuntimeException;

            if (exception2 == null)
            {
                exception2 = ConvertToRuntimeException(exception, funcContext.CurrentPosition);
            }
            else
            {
                InterpreterError.UpdateExceptionErrorRecordPosition(exception2, funcContext.CurrentPosition);
            }
            RuntimeException.LockStackTrace(exception2);
            ExecutionContext context        = funcContext._executionContext;
            Pipe             outputPipe     = funcContext._outputPipe;
            IScriptExtent    scriptPosition = exception2.ErrorRecord.InvocationInfo.ScriptPosition;

            SetErrorVariables(scriptPosition, exception2, context, outputPipe);
            context.QuestionMarkVariableValue = false;
            bool flag = funcContext._traps.Any <Tuple <Type[], Action <FunctionContext>[], Type[]> >() && (funcContext._traps.Last <Tuple <Type[], Action <FunctionContext>[], Type[]> >().Item2 != null);

            if (!flag && !NeedToQueryForActionPreference(exception2, context))
            {
                throw exception2;
            }
            if (flag)
            {
                preference = ProcessTraps(funcContext, exception2);
            }
            else
            {
                preference = QueryForAction(exception2, exception2.Message, context);
            }
            context.QuestionMarkVariableValue = false;
            switch (preference)
            {
            case ActionPreference.SilentlyContinue:
            case ActionPreference.Ignore:
                return;

            case ActionPreference.Stop:
                exception2.SuppressPromptInInterpreter = true;
                throw exception2;
            }
            if (!flag && exception2.WasThrownFromThrowStatement)
            {
                throw exception2;
            }
            bool flag2 = ReportErrorRecord(scriptPosition, exception2, context);

            context.QuestionMarkVariableValue = false;
            if (!flag2)
            {
                throw exception2;
            }
        }