2 // System.Web.HttpException
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Patrik Torstensson
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 using System.Runtime.Serialization;
14 using System.Runtime.InteropServices;
16 using System.Web.Compilation;
17 using System.Web.Util;
24 public class HttpException : ExternalException
30 public HttpException () : base ()
34 public HttpException (string sMessage) : base (sMessage)
38 public HttpException (string sMessage, Exception InnerException)
39 : base (sMessage, InnerException)
43 public HttpException (int iHttpCode, string sMessage) : base (sMessage)
45 http_code = iHttpCode;
49 protected HttpException (SerializationInfo info, StreamingContext sc) : base (info, sc)
51 http_code = info.GetInt32 ("_httpCode");
54 public override void GetObjectData (SerializationInfo info, StreamingContext context)
56 base.GetObjectData (info, context);
57 info.AddValue ("_httpCode", http_code);
61 public HttpException (int iHttpCode, string sMessage, int iHR) : base (sMessage)
63 http_code = iHttpCode;
67 public HttpException (string sMessage, int iHR) : base (sMessage)
72 public HttpException (int iHttpCode,
74 Exception InnerException)
75 : base (sMessage, InnerException)
77 http_code = iHttpCode;
80 internal HttpException (string message, string fileName)
83 this.fileName = fileName;
87 public string GetHtmlErrorMessage ()
89 if (!(this.InnerException is HtmlizedException))
90 return GetDefaultErrorMessage ();
92 return GetHtmlizedErrorMessage ();
95 string GetDefaultErrorMessage ()
97 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
98 builder.Append ("Error");
100 builder.Append (" " + http_code);
102 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
103 "<h1><font color=\"red\">Server error in '{0}' " +
104 "application</font></h1><hr>\r\n",
105 HttpRuntime.AppDomainAppVirtualPath);
107 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n", Message);
108 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", "Error processing request.");
109 builder.Append ("<b>Error Message: </b>");
111 builder.AppendFormat ("HTTP {0}. ", http_code);
113 builder.AppendFormat ("{0}\r\n<p>\r\n", HtmlEncode (this.Message));
115 if (InnerException != null) {
116 builder.AppendFormat ("<b>Stack Trace: </b>");
117 builder.Append ("<table summary=\"Stack Trace\" width=\"100%\" " +
118 "bgcolor=\"#ffffc\">\r\n<tr><td>");
119 WriteTextAsCode (builder, InnerException.ToString ());
120 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
123 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
124 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HttpUtility.HtmlEncode (this.ToString ()));
126 return builder.ToString ();
129 static string HtmlEncode (string s)
134 string res = HttpUtility.HtmlEncode (s);
135 return res.Replace ("\r\n", "<br />");
138 string GetHtmlizedErrorMessage ()
140 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
141 HtmlizedException exc = (HtmlizedException) this.InnerException;
142 builder.Append (exc.Title);
143 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
144 "<h1><font color=\"red\">Server Error in '{0}' " +
145 "Application</font></h1><hr>\r\n",
146 HttpRuntime.AppDomainAppVirtualPath);
148 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n", exc.Title);
149 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.Description));
150 builder.AppendFormat ("<b>Error message: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.ErrorMessage));
152 if (exc.FileName != null)
153 builder.AppendFormat ("<b>File name: </b> {0}", HtmlEncode (exc.FileName));
155 if (exc.FileText != null) {
156 if (exc.SourceFile != exc.FileName)
157 builder.AppendFormat ("<p><b>Source File: </b>{0}", exc.SourceFile);
159 if (exc is ParseException) {
160 builder.Append (" <b>Line: <b>");
161 builder.Append (exc.ErrorLines [0]);
164 builder.Append ("\r\n<p>\r\n");
166 if (exc is ParseException) {
167 builder.Append ("<b>Source Error: </b>\r\n");
168 builder.Append ("<table summary=\"Source error\" width=\"100%\"" +
169 " bgcolor=\"#ffffc\">\r\n<tr><td>");
170 WriteSource (builder, exc);
171 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
173 builder.Append ("<table summary=\"Source file\" width=\"100%\" " +
174 "bgcolor=\"#ffffc\">\r\n<tr><td>");
175 WriteSource (builder, exc);
176 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
180 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
181 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HtmlEncode (exc.ToString ()));
182 return builder.ToString ();
185 static void WriteTextAsCode (StringBuilder builder, string text)
187 builder.Append ("<code><pre>\r\n");
188 builder.AppendFormat ("{0}", HtmlEncode (text));
189 builder.Append ("</pre></code>\r\n");
192 static void WriteSource (StringBuilder builder, HtmlizedException e)
194 builder.Append ("<code><pre>");
195 if (e is CompilationException)
196 WriteCompilationSource (builder, e);
198 WritePageSource (builder, e);
200 builder.Append ("</pre></code>\r\n");
203 static void WriteCompilationSource (StringBuilder builder, HtmlizedException e)
205 int [] a = e.ErrorLines;
211 if (a != null && a.Length > 0)
214 TextReader reader = new StringReader (e.FileText);
215 while ((s = reader.ReadLine ()) != null) {
219 builder.Append ("<span style=\"color: red\">");
221 builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s));
223 if (line == errline) {
224 builder.Append ("</span>");
225 errline = (++index < a.Length) ? a [index] : 0;
230 static void WritePageSource (StringBuilder builder, HtmlizedException e)
234 int beginerror = e.ErrorLines [0];
235 int enderror = e.ErrorLines [1];
236 int begin = beginerror - 3;
237 int end = enderror + 3;
241 TextReader reader = new StringReader (e.FileText);
242 while ((s = reader.ReadLine ()) != null) {
250 if (beginerror == line)
251 builder.Append ("<span style=\"color: red\">");
253 builder.AppendFormat ("{0}\r\n", HtmlEncode (s));
255 if (enderror <= line) {
256 builder.Append ("</span>");
257 enderror = end + 1; // one shot
262 [MonoTODO("Check error type and Set the correct error code")]
263 public int GetHttpCode ()
268 public static HttpException CreateFromLastError (string message)
270 WebTrace.WriteLine ("CreateFromLastError");
271 return new HttpException (message, 0);