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)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
37 using System.Web.Util;
39 using System.Web.Compilation;
45 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50 public class HttpException : ExternalException
54 public HttpException ()
58 public HttpException (string message)
63 public HttpException (string message, Exception innerException)
64 : base (message, innerException)
68 public HttpException (int httpCode, string message) : base (message)
74 protected HttpException (SerializationInfo info, StreamingContext context)
75 : base (info, context)
77 http_code = info.GetInt32 ("_httpCode");
80 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
81 public override void GetObjectData (SerializationInfo info, StreamingContext context)
83 base.GetObjectData (info, context);
84 info.AddValue ("_httpCode", http_code);
88 public HttpException (int httpCode, string message, int hr)
94 public HttpException (string message, int hr)
99 public HttpException (int httpCode, string message, Exception innerException)
100 : base (message, innerException)
102 http_code = httpCode;
105 public string GetHtmlErrorMessage ()
107 if (!(this.InnerException is HtmlizedException))
108 return GetDefaultErrorMessage ();
110 return GetHtmlizedErrorMessage ();
113 internal virtual string Description {
114 get { return "Error processing request."; }
117 string GetDefaultErrorMessage ()
119 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
120 builder.Append ("Error");
122 builder.Append (" " + http_code);
124 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
125 "<h1><font color=\"red\">Server error in '{0}' " +
126 "application</font></h1><hr>\r\n",
127 HtmlEncode (HttpRuntime.AppDomainAppVirtualPath));
129 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n",
130 HtmlEncode (Message));
132 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", Description);
133 builder.Append ("<b>Error Message: </b>");
135 builder.AppendFormat ("HTTP {0}. ", http_code);
137 builder.AppendFormat ("{0}\r\n<p>\r\n", HtmlEncode (this.Message));
139 if (InnerException != null) {
140 builder.AppendFormat ("<b>Stack Trace: </b>");
141 builder.Append ("<table summary=\"Stack Trace\" width=\"100%\" " +
142 "bgcolor=\"#ffffc\">\r\n<tr><td>");
143 WriteTextAsCode (builder, InnerException.ToString ());
144 #if TARGET_J2EE //Required, because toString of Java doesn't print stackTrace
145 WriteTextAsCode (builder, InnerException.StackTrace);
147 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
150 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
151 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HttpUtility.HtmlEncode (this.ToString ()));
152 #if TARGET_J2EE //Required, because toString of Java doesn't print stackTrace
153 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HttpUtility.HtmlEncode (this.StackTrace));
156 return builder.ToString ();
159 static string HtmlEncode (string s)
164 string res = HttpUtility.HtmlEncode (s);
165 return res.Replace ("\r\n", "<br />");
168 string GetHtmlizedErrorMessage ()
170 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
171 HtmlizedException exc = (HtmlizedException) this.InnerException;
172 builder.Append (exc.Title);
173 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
174 "<h1><font color=\"red\">Server Error in '{0}' " +
175 "Application</font></h1><hr>\r\n",
176 HttpRuntime.AppDomainAppVirtualPath);
178 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n", exc.Title);
179 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.Description));
180 builder.AppendFormat ("<b>Error message: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.ErrorMessage));
182 if (exc.FileName != null)
183 builder.AppendFormat ("<b>File name: </b> {0}", HtmlEncode (exc.FileName));
185 if (exc.FileText != null) {
186 if (exc.SourceFile != exc.FileName)
187 builder.AppendFormat ("<p><b>Source File: </b>{0}", exc.SourceFile);
189 builder.Append ("\r\n<p>\r\n");
191 builder.Append ("<table summary=\"Source file\" width=\"100%\" " +
192 "bgcolor=\"#ffffc\">\r\n<tr><td>");
193 WriteSource (builder, exc);
194 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
197 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
198 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HtmlEncode (exc.ToString ()));
199 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HtmlEncode (exc.StackTrace));
200 return builder.ToString ();
203 string GetHtmlizedErrorMessage ()
205 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
206 HtmlizedException exc = (HtmlizedException) this.InnerException;
207 builder.Append (exc.Title);
208 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
209 "<h1><font color=\"red\">Server Error in '{0}' " +
210 "Application</font></h1><hr>\r\n",
211 HttpRuntime.AppDomainAppVirtualPath);
213 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n", exc.Title);
214 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.Description));
215 builder.AppendFormat ("<b>Error message: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.ErrorMessage));
217 if (exc.FileName != null)
218 builder.AppendFormat ("<b>File name: </b> {0}", HtmlEncode (exc.FileName));
220 if (exc.FileText != null) {
221 if (exc.SourceFile != exc.FileName)
222 builder.AppendFormat ("<p><b>Source File: </b>{0}", exc.SourceFile);
224 if (exc is ParseException) {
225 builder.Append (" <b>Line: <b>");
226 builder.Append (exc.ErrorLines [0]);
229 builder.Append ("\r\n<p>\r\n");
231 if (exc is ParseException) {
232 builder.Append ("<b>Source Error: </b>\r\n");
233 builder.Append ("<table summary=\"Source error\" width=\"100%\"" +
234 " bgcolor=\"#ffffc\">\r\n<tr><td>");
235 WriteSource (builder, exc);
236 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
238 builder.Append ("<table summary=\"Source file\" width=\"100%\" " +
239 "bgcolor=\"#ffffc\">\r\n<tr><td>");
240 WriteSource (builder, exc);
241 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
245 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
246 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HtmlEncode (exc.ToString ()));
247 return builder.ToString ();
250 static void WriteTextAsCode (StringBuilder builder, string text)
252 builder.Append ("<code><pre>\r\n");
253 builder.AppendFormat ("{0}", HtmlEncode (text));
254 builder.Append ("</pre></code>\r\n");
258 static void WriteSource (StringBuilder builder, HtmlizedException e)
260 builder.Append ("<code><pre>");
261 WritePageSource (builder, e);
262 builder.Append ("</pre></code>\r\n");
266 static void WriteSource (StringBuilder builder, HtmlizedException e)
268 builder.Append ("<code><pre>");
269 if (e is CompilationException)
270 WriteCompilationSource (builder, e);
272 WritePageSource (builder, e);
274 builder.Append ("</pre></code>\r\n");
278 static void WriteCompilationSource (StringBuilder builder, HtmlizedException e)
280 int [] a = e.ErrorLines;
286 if (a != null && a.Length > 0)
289 TextReader reader = new StringReader (e.FileText);
290 while ((s = reader.ReadLine ()) != null) {
294 builder.Append ("<span style=\"color: red\">");
296 builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s));
298 if (line == errline) {
299 builder.Append ("</span>");
300 errline = (++index < a.Length) ? a [index] : 0;
305 static void WritePageSource (StringBuilder builder, HtmlizedException e)
309 int beginerror = e.ErrorLines [0];
310 int enderror = e.ErrorLines [1];
311 int begin = beginerror - 3;
312 int end = enderror + 3;
316 TextReader reader = new StringReader (e.FileText);
317 while ((s = reader.ReadLine ()) != null) {
325 if (beginerror == line)
326 builder.Append ("<span style=\"color: red\">");
328 builder.AppendFormat ("{0}\r\n", HtmlEncode (s));
330 if (enderror <= line) {
331 builder.Append ("</span>");
332 enderror = end + 1; // one shot
337 public int GetHttpCode ()
342 public static HttpException CreateFromLastError (string message)
344 WebTrace.WriteLine ("CreateFromLastError");
345 return new HttpException (message, 0);