// // System.Web.HttpException // // Authors: // Patrik Torstensson (Patrik.Torstensson@labs2.com) // Gonzalo Paniagua Javier (gonzalo@ximian.com) // // (c) 2002 Patrik Torstensson // (c) 2003 Ximian, Inc. (http://www.ximian.com) // using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.InteropServices; using System.Text; using System.Web.Compilation; using System.Web.Util; namespace System.Web { #if NET_1_2 [Serializable] #endif public class HttpException : ExternalException { int http_code = 500; int hr; string fileName; public HttpException () : base () { } public HttpException (string sMessage) : base (sMessage) { } public HttpException (string sMessage, Exception InnerException) : base (sMessage, InnerException) { } public HttpException (int iHttpCode, string sMessage) : base (sMessage) { http_code = iHttpCode; } #if NET_1_2 protected HttpException (SerializationInfo info, StreamingContext sc) : base (info, sc) { http_code = info.GetInt32 ("_httpCode"); } public override void GetObjectData (SerializationInfo info, StreamingContext context) { base.GetObjectData (info, context); info.AddValue ("_httpCode", http_code); } #endif public HttpException (int iHttpCode, string sMessage, int iHR) : base (sMessage) { http_code = iHttpCode; hr = iHR; } public HttpException (string sMessage, int iHR) : base (sMessage) { hr = iHR; } public HttpException (int iHttpCode, string sMessage, Exception InnerException) : base (sMessage, InnerException) { http_code = iHttpCode; } internal HttpException (string message, string fileName) : base (message) { this.fileName = fileName; } public string GetHtmlErrorMessage () { if (!(this.InnerException is HtmlizedException)) return GetDefaultErrorMessage (); return GetHtmlizedErrorMessage (); } string GetDefaultErrorMessage () { StringBuilder builder = new StringBuilder ("\r\n"); builder.Append ("Error"); if (http_code != 0) builder.Append (" " + http_code); builder.AppendFormat ("" + "

Server error in '{0}' " + "application


\r\n", HttpRuntime.AppDomainAppVirtualPath); builder.AppendFormat ("

{0}

\r\n", Message); builder.AppendFormat ("Description: {0}\r\n

\r\n", "Error processing request."); builder.Append ("Error Message: "); if (http_code != 0) builder.AppendFormat ("HTTP {0}. ", http_code); builder.AppendFormat ("{0}\r\n

\r\n", HtmlEncode (this.Message)); if (InnerException != null) { builder.AppendFormat ("Stack Trace: "); builder.Append ("\r\n\r\n
"); WriteTextAsCode (builder, InnerException.ToString ()); builder.Append ("
\r\n

\r\n"); } builder.Append ("


\r\n\r\n\r\n"); builder.AppendFormat ("\r\n", HttpUtility.HtmlEncode (this.ToString ())); return builder.ToString (); } static string HtmlEncode (string s) { if (s == null) return s; string res = HttpUtility.HtmlEncode (s); return res.Replace ("\r\n", "
"); } string GetHtmlizedErrorMessage () { StringBuilder builder = new StringBuilder ("\r\n"); HtmlizedException exc = (HtmlizedException) this.InnerException; builder.Append (exc.Title); builder.AppendFormat ("" + "

Server Error in '{0}' " + "Application


\r\n", HttpRuntime.AppDomainAppVirtualPath); builder.AppendFormat ("

{0}

\r\n", exc.Title); builder.AppendFormat ("Description: {0}\r\n

\r\n", HtmlEncode (exc.Description)); builder.AppendFormat ("Error message: {0}\r\n

\r\n", HtmlEncode (exc.ErrorMessage)); if (exc.FileName != null) builder.AppendFormat ("File name: {0}", HtmlEncode (exc.FileName)); if (exc.FileText != null) { if (exc.SourceFile != exc.FileName) builder.AppendFormat ("

Source File: {0}", exc.SourceFile); if (exc is ParseException) { builder.Append ("    Line: "); builder.Append (exc.ErrorLines [0]); } builder.Append ("\r\n

\r\n"); if (exc is ParseException) { builder.Append ("Source Error: \r\n"); builder.Append ("\r\n\r\n
"); WriteSource (builder, exc); builder.Append ("
\r\n

\r\n"); } else { builder.Append ("\r\n\r\n
"); WriteSource (builder, exc); builder.Append ("
\r\n

\r\n"); } } builder.Append ("


\r\n\r\n\r\n"); builder.AppendFormat ("\r\n", HtmlEncode (exc.ToString ())); return builder.ToString (); } static void WriteTextAsCode (StringBuilder builder, string text) { builder.Append ("
\r\n");
			builder.AppendFormat ("{0}", HtmlEncode (text));
			builder.Append ("
\r\n"); } static void WriteSource (StringBuilder builder, HtmlizedException e) { builder.Append ("
");
			if (e is CompilationException)
				WriteCompilationSource (builder, e);
			else
				WritePageSource (builder, e);

			builder.Append ("
\r\n"); } static void WriteCompilationSource (StringBuilder builder, HtmlizedException e) { int [] a = e.ErrorLines; string s; int line = 0; int index = 0; int errline = 0; if (a != null && a.Length > 0) errline = a [0]; TextReader reader = new StringReader (e.FileText); while ((s = reader.ReadLine ()) != null) { line++; if (errline == line) builder.Append (""); builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s)); if (line == errline) { builder.Append (""); errline = (++index < a.Length) ? a [index] : 0; } } } static void WritePageSource (StringBuilder builder, HtmlizedException e) { string s; int line = 0; int beginerror = e.ErrorLines [0]; int enderror = e.ErrorLines [1]; int begin = beginerror - 3; int end = enderror + 3; if (begin <= 0) begin = 1; TextReader reader = new StringReader (e.FileText); while ((s = reader.ReadLine ()) != null) { line++; if (line < begin) continue; if (line > end) break; if (beginerror == line) builder.Append (""); builder.AppendFormat ("{0}\r\n", HtmlEncode (s)); if (enderror <= line) { builder.Append (""); enderror = end + 1; // one shot } } } [MonoTODO("Check error type and Set the correct error code")] public int GetHttpCode () { return http_code; } public static HttpException CreateFromLastError (string message) { WebTrace.WriteLine ("CreateFromLastError"); return new HttpException (message, 0); } } }