Exemple #1
0
            public DynamicExceptionInfo(Exception e)
            {
                ContractUtils.RequiresNotNull(e, "e");

                _exception          = e;
                _dynamicStackFrames = ScriptingRuntimeHelpers.GetDynamicStackFrames(e);

                // We can get the file name and line number from either the
                // DynamicStackFrame or from a SyntaxErrorException
                SyntaxErrorException se = e as SyntaxErrorException;

                if (null != se)
                {
                    _sourceFileName = se.GetSymbolDocumentName();
                    _sourceLine     = se.Line;
                }
                else if (_dynamicStackFrames != null && _dynamicStackFrames.Length > 0)
                {
                    _sourceFileName = _dynamicStackFrames[0].GetFileName();
                    _sourceLine     = _dynamicStackFrames[0].GetFileLineNumber();
                }

                // Try to get the ScriptEngine from the source file's extension;
                // if that fails just use the current ScriptEngine
                ScriptEngine engine = null;

                try {
                    if (_sourceFileName != null)
                    {
                        var extension = System.IO.Path.GetExtension(_sourceFileName);
                        _runtime.TryGetEngineByFileExtension(extension, out engine);
                    }
                    else
                    {
                        throw new Exception();
                    }
                } catch {
                    if (DynamicApplication.Current.Engine != null)
                    {
                        engine = DynamicApplication.Current.Engine.Engine;
                    }
                }

                // If we have the file name and the engine, use ExceptionOperations
                // to generate the exception message. Otherwise, create it by hand
                if (_sourceFileName != null && engine != null)
                {
                    ExceptionOperations es = engine.GetService <ExceptionOperations>();
                    es.GetExceptionMessage(_exception, out _message, out _errorTypeName);
                }
                else
                {
                    _errorTypeName = _exception.GetType().Name;
                    _message       = _errorTypeName + ": " + _exception.Message;
                }
            }