public void Ensure_CompilationDiagnostics_FormattedMessage_Matches_CompilationErrors() { var exception = new TemplateCompilationException("Error message", new TemplateCompilationDiagnostic[] { new TemplateCompilationDiagnostic("diagnosticMessage", "diagnosticFormattedMessage", new FileLinePositionSpan("path", new LinePosition(3, 1), new LinePosition(4, 2))) }); Assert.NotEmpty(exception.CompilationDiagnostics); Assert.NotEmpty(exception.CompilationErrors); Assert.Equal(1, exception.CompilationDiagnostics.Count); Assert.Equal(1, exception.CompilationErrors.Count); var firstDiagnostic = exception.CompilationDiagnostics[0]; Assert.Equal("diagnosticMessage", firstDiagnostic.ErrorMessage); Assert.Equal("diagnosticFormattedMessage", firstDiagnostic.FormattedMessage); Assert.Equal("path", firstDiagnostic.LineSpan?.Path); Assert.Equal(3, firstDiagnostic.LineSpan?.StartLinePosition.Line); Assert.Equal(1, firstDiagnostic.LineSpan?.StartLinePosition.Character); Assert.Equal(4, firstDiagnostic.LineSpan?.EndLinePosition.Line); Assert.Equal(2, firstDiagnostic.LineSpan?.EndLinePosition.Character); Assert.Equal(firstDiagnostic.FormattedMessage, exception.CompilationErrors[0]); }
static public string GetExceptionMessage(TemplateCompilationException ex) { var result = new StringBuilder(""); foreach (var err in ex.CompilerErrors) { result.AppendFormat("{0}\r\nLine {1} Column {2} Error Number {3}\r\n", err.ErrorText, err.Line, err.Column, err.ErrorNumber); } return(result.ToString()); }
static public string GetExceptionMessage(TemplateCompilationException ex) { string result = ""; foreach (var error in ex.Errors) { result += error.ErrorText + "\r\n"; } return(result); }
static public string GetExceptionMessage(TemplateCompilationException ex) { var result = new StringBuilder(""); var firstError = ""; foreach (var err in ex.CompilerErrors) { if (string.IsNullOrEmpty(firstError) && err.Line > 0) { firstError = err.ErrorText + "\r\n\r\n"; } result.AppendFormat("{0}\r\nLine {1} Column {2} Error Number {3}\r\n", err.ErrorText, err.Line, err.Column, err.ErrorNumber); } return(firstError + result.ToString()); }
public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { var templateSource = builder.Build(options.Usings); var typeBuilder = new BooTemplateTypeBuilder(options); var templateType = typeBuilder.Build(templateSource, builder.ClassName); if (templateType == null) { var path = ListExtensions.Last(viewSourceReader.ViewSources).Path; TemplateCompilationException.Throw(typeBuilder.CompilerResults, typeBuilder.Source, path); } return(new TemplateFactory(templateType)); }
public static string GetError(TemplateCompilationException exception) { var errorStrBuilder = new StringBuilder(); foreach (var compilerError in exception.CompilerErrors) { errorStrBuilder.AppendLine(); errorStrBuilder.AppendFormat("Error Message : {0} {1}", compilerError.ErrorText, compilerError.ErrorNumber); errorStrBuilder.AppendLine(); errorStrBuilder.AppendLine("################"); errorStrBuilder.AppendFormat("Source Line {0} :{1}\r\n", compilerError.Line - 1, GetLine(exception.SourceCode, compilerError.Line - 1)); errorStrBuilder.AppendFormat("Error Line {0} :{1}\r\n", compilerError.Line, GetLine(exception.SourceCode, compilerError.Line)); errorStrBuilder.AppendFormat("Source Line {0} :{1}\r\n", compilerError.Line + 1, GetLine(exception.SourceCode, compilerError.Line + 1)); errorStrBuilder.AppendLine("################"); } return(errorStrBuilder.ToString()); }
/// <summary> /// Displays a custom version of this form to display Razor template compile errors /// </summary> public static DialogResult ShowTemplateError(TemplateCompilationException E, string TemplateFile) { using (ExceptionForm F = new ExceptionForm(E, true)) { // Get our relative path from the program root string fileRelativePath = TemplateFile.Replace(Program.RootPath, ""); // Set the window properties F.WindowTitle = "Compile Error"; F.HeaderText = "Template Compile Error"; F.Message = "An error occured while trying to compile the file \"" + Path.GetFileName(fileRelativePath) + "\""; F.ImgIcon = Properties.Resources.vistaWarning; if (E.CompilerErrors.Count > 0) { StringBuilder builder = new StringBuilder(); // Append each error's details into the Details stringbuilder foreach (RazorEngineCompilerError error in E.CompilerErrors) { builder.AppendLine("Compile Error:"); builder.AppendLine(error.ErrorText); builder.AppendLine(); builder.AppendLine("Error #: " + error.ErrorNumber); builder.AppendLine("File: " + fileRelativePath); builder.AppendLine("Line: " + error.Line); builder.AppendLine("Column: " + error.Column); builder.AppendLine(); } // Set the Details pane contents F.ExceptionDetails.Text = builder.ToString(); } else { F.ExceptionDetails.Text = E.Message; } return(F.ShowDialog(false)); } }
public void Ensure_InitalizedWtihErrors_FormattedMessage_Matches_CompilationErrors() { var exception = new TemplateCompilationException("Error message", new string[] { "formattedMessage" }); Assert.NotEmpty(exception.CompilationDiagnostics); Assert.NotEmpty(exception.CompilationErrors); Assert.Equal(1, exception.CompilationDiagnostics.Count); Assert.Equal(1, exception.CompilationErrors.Count); var firstDiagnostic = exception.CompilationDiagnostics[0]; Assert.Equal("formattedMessage", firstDiagnostic.ErrorMessage); Assert.Equal("formattedMessage", firstDiagnostic.FormattedMessage); Assert.Null(firstDiagnostic.LineSpan); Assert.Equal(firstDiagnostic.FormattedMessage, exception.CompilationErrors[0]); }
public TemplateFactory Compile(ViewSourceReader viewSourceReader, TemplateOptions options, TemplateClassBuilder builder) { var typeBuilder = CreateTemplateTypeBuilder(options); //TODO: leaky abstraction var classBuilder = (CodeDomClassBuilder)builder; var provider = GetCodeDomProvider(typeBuilder.ProviderOptions); classBuilder.CodeDomProvider = provider; typeBuilder.CodeDomProvider = provider; var templateSource = classBuilder.Build(options.Usings); var templateType = typeBuilder.Build(templateSource, classBuilder.ClassName); if (templateType == null) { var viewSources = viewSourceReader.ViewSources; TemplateCompilationException.Throw(typeBuilder.CompilerResults, typeBuilder.Source, ListExtensions.Last(viewSources).Path); } return(new TemplateFactory(templateType)); }
public async Task Throw_With_CompilationErrors_On_Failed_BuildAsync() { var compiler = new RoslynCompilationService(new DefaultMetadataReferenceManager(), Assembly.GetEntryAssembly()); var template = new TestGeneratedRazorTemplate("key", "public class Test { error }"); TemplateCompilationException ex = null; try { await compiler.CompileAsync(template); } catch (TemplateCompilationException e) { ex = e; } Assert.NotNull(ex); Assert.NotEmpty(ex.CompilationErrors); Assert.Equal(1, ex.CompilationErrors.Count); }