private void EmbedExecutionExceptionSourceCode(Exception ex) { if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException || ex is ThreadInterruptedException) { return; } var stackTrace = new StackTrace(ex, true); string fullFilePath = PathUtil.Resolve(VirtualPath); foreach (var frame in stackTrace.GetFrames()) { string fileName = frame.GetFileName(); if (fileName != null && fileName.StartsWith(fullFilePath, StringComparison.InvariantCultureIgnoreCase)) { var sourceCode = C1File.ReadAllLines(fileName); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, sourceCode, frame.GetFileLineNumber()); return; } } }
private void EmbedExceptionSourceCode(Exception ex) { if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException || ex is ThreadInterruptedException) { return; } var stackTrace = new StackTrace(ex, true); foreach (var frame in stackTrace.GetFrames()) { string fileName = frame.GetFileName(); if (fileName != null && File.Exists(fileName)) { var sourceCode = C1File.ReadAllLines(fileName); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, sourceCode, frame.GetFileLineNumber()); return; } } }
private void EmbedSourceCodeInformation(HttpCompileException ex) { var compilationErrors = ex.Results.Errors; if (!compilationErrors.HasErrors) { return; } CompilerError firstError = null; for (int i = 0; i < compilationErrors.Count; i++) { if (!compilationErrors[i].IsWarning) { firstError = compilationErrors[i]; break; } } Verify.IsNotNull(firstError, "Failed to finding an error in the compiler results."); // Not showing source code of not related files if (!firstError.FileName.StartsWith(PathUtil.Resolve(VirtualPath), StringComparison.OrdinalIgnoreCase)) { return; } string[] sourceCode = C1File.ReadAllLines(firstError.FileName); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, sourceCode, firstError.Line); }
private void EmbedSourceCodeInformation(HttpParseException ex) { // Not showing source code of not related files if (!ex.FileName.StartsWith(PathUtil.Resolve(VirtualPath), StringComparison.OrdinalIgnoreCase)) { return; } string[] sourceCode = C1File.ReadAllLines(ex.FileName); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, sourceCode, ex.Line); }
private XhtmlDocument ParseOutput(string xhtml) { try { return(XhtmlDocument.ParseXhtmlFragment(xhtml)); } catch (XmlException ex) { string[] codeLines = xhtml.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, codeLines, ex.LineNumber); throw; } }
/// <summary> /// Checks whether an exception has to be re-thrown, if not - writes it to the log /// and returns markup that should be inserted as function's result. /// </summary> /// <param name="functionName">The name of the function</param> /// <param name="exception">The exception</param> /// <param name="logTitle">The log entry title.</param> /// <param name="errorBoxHtml">The markup that should be inserted instead of the function call</param> /// <returns><value>True</value> if the exception has to be re-thrown, <value>False</value> otherwise.</returns> public bool ProcessException(string functionName, Exception exception, string logTitle, out XElement errorBoxHtml) { if (!SuppressXhtmlExceptions || exception is ThreadAbortException || exception is ThreadInterruptedException || exception is AppDomainUnloadedException || exception is OutOfMemoryException) { errorBoxHtml = null; return(false); } Log.LogError("Function: " + functionName, exception); errorBoxHtml = XhtmlErrorFormatter.GetErrorDescriptionHtmlElement(exception, functionName); return(true); }
object IFunction.Execute(ParameterList parameters, FunctionContextContainer context) { if (_errors.CompileErrors.Count > 0) { var error = _errors.CompileErrors[0]; var exception = new InvalidOperationException("{1} Line {0}: {2}".FormatWith(error.Item1, error.Item2, error.Item3)); if (_sourceCode == null) { string filepath = Path.Combine(PathUtil.Resolve(GlobalSettingsFacade.InlineCSharpFunctionDirectory), _function.CodePath); if (C1File.Exists(filepath)) { _sourceCode = C1File.ReadAllLines(filepath); } else { _sourceCode = new string[0]; } } if (_sourceCode.Length > 0) { XhtmlErrorFormatter.EmbedSourceCodeInformation(exception, _sourceCode, error.Item1); } throw exception; } if (_errors.LoadingException != null) { throw _errors.LoadingException; } throw new InvalidOperationException("Function wasn't loaded due to compilation errors"); }
/// <summary> /// Executes the razor page. /// </summary> /// <param name="webPage">The web page.</param> /// <param name="setParameters">Delegate to set the parameters.</param> /// <param name="resultType">The type of the result.</param> /// <param name="functionContextContainer">The function context container</param> /// <returns></returns> public static object ExecuteRazorPage( WebPageBase webPage, Action<WebPageBase> setParameters, Type resultType, FunctionContextContainer functionContextContainer) { HttpContext currentContext = HttpContext.Current; var startPage = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" }); // IEnumerable<PageExecutionListener> pageExecutionListeners; HttpContextBase httpContext; if (currentContext == null) { httpContext = new NoHttpRazorContext(); // pageExecutionListeners = new PageExecutionListener[0]; } else { httpContext = new HttpContextWrapper(currentContext); // pageExecutionListeners = httpContext.PageInstrumentation.ExecutionListeners; } var pageContext = new WebPageContext(httpContext, webPage, startPage); if (functionContextContainer != null) { pageContext.PageData.Add(PageContext_FunctionContextContainer, functionContextContainer); } if (setParameters != null) { setParameters(webPage); } var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { //// PageExecutionContext enables "Browser Link" support //var pageExecutionContext = new PageExecutionContext //{ // TextWriter = writer, // VirtualPath = PathUtil.Resolve(webPage.VirtualPath), // StartPosition = 0, // IsLiteral = true //}; //pageExecutionListeners.ForEach(l => l.BeginContext(pageExecutionContext)); webPage.ExecutePageHierarchy(pageContext, writer); //pageExecutionListeners.ForEach(l => l.EndContext(pageExecutionContext)); } string output = sb.ToString(); if (resultType == typeof(XhtmlDocument)) { if (string.IsNullOrWhiteSpace(output)) return new XhtmlDocument(); try { return XhtmlDocument.ParseXhtmlFragment(output); } catch (XmlException ex) { string[] codeLines = output.Split(new [] { Environment.NewLine, "\n" }, StringSplitOptions.None); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, codeLines, ex.LineNumber); throw; } } return ValueTypeConverter.Convert(output, resultType); }