/// <summary> /// Render the line with the error plus some context lines /// </summary> private static string FormatSourceCode(DynamicExceptionInfo err) { var sourceFile = err.SourceFileName; int line = err.SourceLine; if (sourceFile == null || line <= 0) { return(""); } Stream stream = null; try { stream = _runtime.Host.PlatformAdaptationLayer.OpenInputFileStream(sourceFile); if (stream == null) { return(""); } } catch (IOException) { return(""); } int maxLen = (line + 2).ToString().Length; var text = new StringBuilder(); using (StreamReader reader = new StreamReader(stream)) { for (int i = 1; i <= line + 2; ++i) { string lineText = reader.ReadLine(); if (null == lineText) { break; } if (i < line - 2) { continue; } string lineNum = i.ToString(); text.Append("Line ").Append(' ', maxLen - lineNum.Length).Append(lineNum).Append(": "); lineText = EscapeHtml(lineText); if (i == line) { text.AppendFormat(_errorLineTemplate, lineText, _errorLineId); } else { text.Append(lineText); } if (i != line + 2) { text.Append("<br />"); } } } return(text.ToString()); }
/// <summary> /// Gets the stack trace using whatever information we have available /// </summary> private static string FormatStackTrace(DynamicExceptionInfo err) { Exception ex = err.Exception; if (ex is SyntaxErrorException) { return(""); // no meaningful stack trace for syntax errors } DynamicStackFrame[] dynamicFrames = err.DynamicStackFrames; if (dynamicFrames != null && dynamicFrames.Length > 0) { // We have a stack trace, either dynamic or directly from the exception StringBuilder html = new StringBuilder(); foreach (DynamicStackFrame frame in dynamicFrames) { if (html.Length != 0) { html.Append("<br />"); } html.AppendFormat( " at {0} in {1}, line {2}", EscapeHtml(frame.GetMethodName()), frame.GetFileName() != null ? EscapeHtml(frame.GetFileName()) : null, frame.GetFileLineNumber() ); } if (Settings.ExceptionDetail) { html.Append("<br/>CLR Stack Trace:<br/>"); html.Append(EscapeHtml(ex.StackTrace != null ? ex.StackTrace : ex.ToString())); } return(html.ToString()); } return(EscapeHtml(ex.StackTrace != null ? ex.StackTrace : ex.ToString())); }
/// <summary> /// Get the dynamic exception info from the CLR exception, and format /// it with the _errorHtmlTemplate. /// </summary> internal static string FormatErrorAsHtml(Exception e) { // Get information about this exception object DynamicExceptionInfo err = new DynamicExceptionInfo(e); return(string.Format( _errorHtmlTemplate, EscapeHtml(err.Message), err.SourceFileName != null ? EscapeHtml(err.SourceFileName) : "", FormatSourceCode(err), EscapeHtml(err.ErrorTypeName), FormatStackTrace(err), _errorReportId, _errorMessageId, _errorSourceId, _errorSourceFileId, _errorSourceCodeId, _errorDetailsId, _errorTypeId, _errorStackTraceId )); }
/// <summary> /// Gets the stack trace using whatever information we have available /// </summary> private static string FormatStackTrace(DynamicExceptionInfo err) { Exception ex = err.Exception; if (ex is SyntaxErrorException) { return ""; // no meaningful stack trace for syntax errors } DynamicStackFrame[] dynamicFrames = err.DynamicStackFrames; if (dynamicFrames != null && dynamicFrames.Length > 0) { // We have a stack trace, either dynamic or directly from the exception StringBuilder html = new StringBuilder(); foreach (DynamicStackFrame frame in dynamicFrames) { if (html.Length != 0) { html.Append("<br />"); } html.AppendFormat( " at {0} in {1}, line {2}", EscapeHtml(frame.GetMethodName()), frame.GetFileName() != null ? EscapeHtml(frame.GetFileName()) : null, frame.GetFileLineNumber() ); } if (Settings.ExceptionDetail) { html.Append("<br/>CLR Stack Trace:<br/>"); html.Append(EscapeHtml(ex.StackTrace != null ? ex.StackTrace : ex.ToString())); } return html.ToString(); } return EscapeHtml(ex.StackTrace != null ? ex.StackTrace : ex.ToString()); }
/// <summary> /// Render the line with the error plus some context lines /// </summary> private static string FormatSourceCode(DynamicExceptionInfo err) { var sourceFile = err.SourceFileName; int line = err.SourceLine; if (sourceFile == null || line <= 0) { return ""; } Stream stream = null; try { stream = _runtime.Host.PlatformAdaptationLayer.OpenInputFileStream(sourceFile); if (stream == null) { return ""; } } catch (IOException) { return ""; } int maxLen = (line + 2).ToString().Length; var text = new StringBuilder(); using (StreamReader reader = new StreamReader(stream)) { for (int i = 1; i <= line + 2; ++i) { string lineText = reader.ReadLine(); if (null == lineText) { break; } if (i < line - 2) { continue; } string lineNum = i.ToString(); text.Append("Line ").Append(' ', maxLen - lineNum.Length).Append(lineNum).Append(": "); lineText = EscapeHtml(lineText); if (i == line) { text.AppendFormat(_errorLineTemplate, lineText, _errorLineId); } else { text.Append(lineText); } if (i != line + 2) { text.Append("<br />"); } } } return text.ToString(); }
/// <summary> /// Get the dynamic exception info from the CLR exception, and format /// it with the _errorHtmlTemplate. /// </summary> internal static string FormatErrorAsHtml(Exception e) { // Get information about this exception object DynamicExceptionInfo err = new DynamicExceptionInfo(e); return string.Format( _errorHtmlTemplate, EscapeHtml(err.Message), err.SourceFileName != null ? EscapeHtml(err.SourceFileName) : "", FormatSourceCode(err), EscapeHtml(err.ErrorTypeName), FormatStackTrace(err), _errorReportId, _errorMessageId, _errorSourceId, _errorSourceFileId, _errorSourceCodeId, _errorDetailsId, _errorTypeId, _errorStackTraceId ); }
private static string FormatErrorAsHtml(Exception e) { // Get information about this exception object DynamicExceptionInfo err = new DynamicExceptionInfo(e); // Template for generating error HTML. Parameters are: // 0 - message // 1 - source file // 2 - source code at error location // 3 - error type // 4 - stack trace return string.Format( _ErrorHtmlTemplate, EscapeHtml(err.Message), err.SourceFileName != null ? EscapeHtml(err.SourceFileName) : "", FormatSourceCode(err), EscapeHtml(err.ErrorTypeName), FormatStackTrace(err)); }