Пример #1
0
    internal void ReadFrameContent(
        StackFrameSourceCodeInfo frame,
        IEnumerable <string> allLines,
        int errorStartLineNumberInFile,
        int errorEndLineNumberInFile)
    {
        // Get the line boundaries in the file to be read and read all these lines at once into an array.
        var preErrorLineNumberInFile  = Math.Max(errorStartLineNumberInFile - _sourceCodeLineCount, 1);
        var postErrorLineNumberInFile = errorEndLineNumberInFile + _sourceCodeLineCount;
        var codeBlock = allLines
                        .Skip(preErrorLineNumberInFile - 1)
                        .Take(postErrorLineNumberInFile - preErrorLineNumberInFile + 1)
                        .ToArray();

        var numOfErrorLines             = (errorEndLineNumberInFile - errorStartLineNumberInFile) + 1;
        var errorStartLineNumberInArray = errorStartLineNumberInFile - preErrorLineNumberInFile;

        frame.PreContextLine = preErrorLineNumberInFile;
        frame.PreContextCode = codeBlock.Take(errorStartLineNumberInArray).ToArray();
        frame.ContextCode    = codeBlock
                               .Skip(errorStartLineNumberInArray)
                               .Take(numOfErrorLines)
                               .ToArray();
        frame.PostContextCode = codeBlock
                                .Skip(errorStartLineNumberInArray + numOfErrorLines)
                                .ToArray();
    }
        private static void AppendStackFrameLocation(StringBuilder text, StackFrameSourceCodeInfo stackFrame)
        {
            text.Append(stackFrame.Function);

            if (!string.IsNullOrEmpty(stackFrame?.File))
            {
                text.Append(" in ");
                text.Append(Path.GetFileName(stackFrame.File));
            }
        }
Пример #3
0
        private Task DisplayCompilationException(
            HttpContext context,
            ICompilationException compilationException)
        {
            var model = new CompilationErrorPageModel
            {
                Options = _options,
            };

            foreach (var compilationFailure in compilationException.CompilationFailures)
            {
                var stackFrames      = new List <StackFrameSourceCodeInfo>();
                var exceptionDetails = new ExceptionDetails
                {
                    StackFrames  = stackFrames,
                    ErrorMessage = compilationFailure.FailureSummary,
                };
                var fileContent = compilationFailure
                                  .SourceFileContent
                                  .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                foreach (var item in compilationFailure.Messages)
                {
                    var frame = new StackFrameSourceCodeInfo
                    {
                        File     = compilationFailure.SourceFilePath,
                        Line     = item.StartLine,
                        Function = string.Empty
                    };

                    _exceptionDetailsProvider.ReadFrameContent(frame, fileContent, item.StartLine, item.EndLine);
                    frame.ErrorDetails = item.Message;

                    stackFrames.Add(frame);
                }

                model.ErrorDetails.Add(exceptionDetails);
                model.CompiledContent.Add(compilationFailure.CompiledContent);
            }

            var errorPage = new CompilationErrorPage
            {
                Model = model
            };

            return(errorPage.ExecuteAsync(context));
        }
Пример #4
0
    internal StackFrameSourceCodeInfo GetStackFrameSourceCodeInfo(string?method, string?filePath, int lineNumber)
    {
        var stackFrame = new StackFrameSourceCodeInfo
        {
            Function = method,
            File     = filePath,
            Line     = lineNumber
        };

        if (string.IsNullOrEmpty(stackFrame.File))
        {
            return(stackFrame);
        }

        IEnumerable <string>?lines = null;

        if (File.Exists(stackFrame.File))
        {
            lines = File.ReadLines(stackFrame.File);
        }
        else
        {
            // Handle relative paths and embedded files
            var fileInfo = _fileProvider.GetFileInfo(stackFrame.File);
            if (fileInfo.Exists)
            {
                // ReadLines doesn't accept a stream. Use ReadLines as its more efficient
                // relative to reading lines via stream reader
                if (!string.IsNullOrEmpty(fileInfo.PhysicalPath))
                {
                    lines = File.ReadLines(fileInfo.PhysicalPath);
                }
                else
                {
                    lines = ReadLines(fileInfo);
                }
            }
        }

        if (lines != null)
        {
            ReadFrameContent(stackFrame, lines, stackFrame.Line, stackFrame.Line);
        }

        return(stackFrame);
    }
Пример #5
0
        public void DisplaysSourceCodeLines_PreAndPostErrorLine(ErrorData errorData)
        {
            // Arrange
            var stackFrame = new StackFrameSourceCodeInfo();

            // Act
            var exceptionDetailProvider = new ExceptionDetailsProvider(
                new PhysicalFileProvider(Directory.GetCurrentDirectory()),
                sourceCodeLineCount: 6);

            exceptionDetailProvider.ReadFrameContent(
                stackFrame,
                errorData.AllLines,
                errorData.ErrorStartLine,
                errorData.ErrorEndLine);

            // Assert
            Assert.Equal(errorData.ExpectedPreContextLine, stackFrame.PreContextLine);
            Assert.Equal(errorData.ExpectedPreErrorCode, stackFrame.PreContextCode);
            Assert.Equal(errorData.ExpectedErrorCode, stackFrame.ContextCode);
            Assert.Equal(errorData.ExpectedPostErrorCode, stackFrame.PostContextCode);
        }
Пример #6
0
        private Task DisplayCompilationException(
            HttpContext context,
            ICompilationException compilationException)
        {
            var model = new CompilationErrorPageModel(_options);

            var errorPage = new CompilationErrorPage(model);

            if (compilationException.CompilationFailures == null)
            {
                return(errorPage.ExecuteAsync(context));
            }

            foreach (var compilationFailure in compilationException.CompilationFailures)
            {
                if (compilationFailure == null)
                {
                    continue;
                }

                var stackFrames      = new List <StackFrameSourceCodeInfo>();
                var exceptionDetails = new ExceptionDetails(compilationFailure.FailureSummary !, stackFrames);
                model.ErrorDetails.Add(exceptionDetails);
                model.CompiledContent.Add(compilationFailure.CompiledContent);

                if (compilationFailure.Messages == null)
                {
                    continue;
                }

                var sourceLines = compilationFailure
                                  .SourceFileContent?
                                  .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                foreach (var item in compilationFailure.Messages)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var frame = new StackFrameSourceCodeInfo
                    {
                        File     = compilationFailure.SourceFilePath,
                        Line     = item.StartLine,
                        Function = string.Empty
                    };

                    if (sourceLines != null)
                    {
                        _exceptionDetailsProvider.ReadFrameContent(frame, sourceLines, item.StartLine, item.EndLine);
                    }

                    frame.ErrorDetails = item.Message;

                    stackFrames.Add(frame);
                }
            }

            return(errorPage.ExecuteAsync(context));
        }