**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web / HttpException.cs
blobe02c46a1125a0c001dca7356ba668ed1fc1b8a20
1 //
2 // System.Web.HttpException
3 //
4 // Authors:
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2002 Patrik Torstensson
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.IO;
34 using System.Runtime.Serialization;
35 using System.Runtime.InteropServices;
36 using System.Text;
37 using System.Web.Compilation;
38 using System.Web.Util;
40 namespace System.Web
42 #if NET_2_0
43 [Serializable]
44 #endif
45 public class HttpException : ExternalException
47 int http_code = 500;
48 int hr;
49 string fileName;
51 public HttpException () : base ()
55 public HttpException (string sMessage) : base (sMessage)
59 public HttpException (string sMessage, Exception InnerException)
60 : base (sMessage, InnerException)
64 public HttpException (int iHttpCode, string sMessage) : base (sMessage)
66 http_code = iHttpCode;
69 #if NET_2_0
70 protected HttpException (SerializationInfo info, StreamingContext sc) : base (info, sc)
72 http_code = info.GetInt32 ("_httpCode");
75 public override void GetObjectData (SerializationInfo info, StreamingContext context)
77 base.GetObjectData (info, context);
78 info.AddValue ("_httpCode", http_code);
80 #endif
82 public HttpException (int iHttpCode, string sMessage, int iHR) : base (sMessage)
84 http_code = iHttpCode;
85 hr = iHR;
88 public HttpException (string sMessage, int iHR) : base (sMessage)
90 hr = iHR;
93 public HttpException (int iHttpCode,
94 string sMessage,
95 Exception InnerException)
96 : base (sMessage, InnerException)
98 http_code = iHttpCode;
101 internal HttpException (string message, string fileName)
102 : base (message)
104 this.fileName = fileName;
108 public string GetHtmlErrorMessage ()
110 if (!(this.InnerException is HtmlizedException))
111 return GetDefaultErrorMessage ();
113 return GetHtmlizedErrorMessage ();
116 internal virtual string Description {
117 get { return "Error processing request."; }
120 string GetDefaultErrorMessage ()
122 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
123 builder.Append ("Error");
124 if (http_code != 0)
125 builder.Append (" " + http_code);
127 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
128 "<h1><font color=\"red\">Server error in '{0}' " +
129 "application</font></h1><hr>\r\n",
130 HtmlEncode (HttpRuntime.AppDomainAppVirtualPath));
132 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n",
133 HtmlEncode (Message));
135 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", Description);
136 builder.Append ("<b>Error Message: </b>");
137 if (http_code != 0)
138 builder.AppendFormat ("HTTP {0}. ", http_code);
140 builder.AppendFormat ("{0}\r\n<p>\r\n", HtmlEncode (this.Message));
142 if (InnerException != null) {
143 builder.AppendFormat ("<b>Stack Trace: </b>");
144 builder.Append ("<table summary=\"Stack Trace\" width=\"100%\" " +
145 "bgcolor=\"#ffffc\">\r\n<tr><td>");
146 WriteTextAsCode (builder, InnerException.ToString ());
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 ()));
153 return builder.ToString ();
156 static string HtmlEncode (string s)
158 if (s == null)
159 return s;
161 string res = HttpUtility.HtmlEncode (s);
162 return res.Replace ("\r\n", "<br />");
165 string GetHtmlizedErrorMessage ()
167 StringBuilder builder = new StringBuilder ("<html>\r\n<title>");
168 HtmlizedException exc = (HtmlizedException) this.InnerException;
169 builder.Append (exc.Title);
170 builder.AppendFormat ("</title><body bgcolor=\"white\">" +
171 "<h1><font color=\"red\">Server Error in '{0}' " +
172 "Application</font></h1><hr>\r\n",
173 HttpRuntime.AppDomainAppVirtualPath);
175 builder.AppendFormat ("<h2><font color=\"maroon\"><i>{0}</i></font></h2>\r\n", exc.Title);
176 builder.AppendFormat ("<b>Description: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.Description));
177 builder.AppendFormat ("<b>Error message: </b>{0}\r\n<p>\r\n", HtmlEncode (exc.ErrorMessage));
179 if (exc.FileName != null)
180 builder.AppendFormat ("<b>File name: </b> {0}", HtmlEncode (exc.FileName));
182 if (exc.FileText != null) {
183 if (exc.SourceFile != exc.FileName)
184 builder.AppendFormat ("<p><b>Source File: </b>{0}", exc.SourceFile);
186 if (exc is ParseException) {
187 builder.Append ("&nbsp;&nbsp;&nbsp;&nbsp;<b>Line: <b>");
188 builder.Append (exc.ErrorLines [0]);
191 builder.Append ("\r\n<p>\r\n");
193 if (exc is ParseException) {
194 builder.Append ("<b>Source Error: </b>\r\n");
195 builder.Append ("<table summary=\"Source error\" width=\"100%\"" +
196 " bgcolor=\"#ffffc\">\r\n<tr><td>");
197 WriteSource (builder, exc);
198 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
199 } else {
200 builder.Append ("<table summary=\"Source file\" width=\"100%\" " +
201 "bgcolor=\"#ffffc\">\r\n<tr><td>");
202 WriteSource (builder, exc);
203 builder.Append ("</td></tr>\r\n</table>\r\n<p>\r\n");
207 builder.Append ("<hr>\r\n</body>\r\n</html>\r\n");
208 builder.AppendFormat ("<!--\r\n{0}\r\n-->\r\n", HtmlEncode (exc.ToString ()));
209 return builder.ToString ();
212 static void WriteTextAsCode (StringBuilder builder, string text)
214 builder.Append ("<code><pre>\r\n");
215 builder.AppendFormat ("{0}", HtmlEncode (text));
216 builder.Append ("</pre></code>\r\n");
219 static void WriteSource (StringBuilder builder, HtmlizedException e)
221 builder.Append ("<code><pre>");
222 if (e is CompilationException)
223 WriteCompilationSource (builder, e);
224 else
225 WritePageSource (builder, e);
227 builder.Append ("</pre></code>\r\n");
230 static void WriteCompilationSource (StringBuilder builder, HtmlizedException e)
232 int [] a = e.ErrorLines;
233 string s;
234 int line = 0;
235 int index = 0;
236 int errline = 0;
238 if (a != null && a.Length > 0)
239 errline = a [0];
241 TextReader reader = new StringReader (e.FileText);
242 while ((s = reader.ReadLine ()) != null) {
243 line++;
245 if (errline == line)
246 builder.Append ("<span style=\"color: red\">");
248 builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s));
250 if (line == errline) {
251 builder.Append ("</span>");
252 errline = (++index < a.Length) ? a [index] : 0;
257 static void WritePageSource (StringBuilder builder, HtmlizedException e)
259 string s;
260 int line = 0;
261 int beginerror = e.ErrorLines [0];
262 int enderror = e.ErrorLines [1];
263 int begin = beginerror - 3;
264 int end = enderror + 3;
265 if (begin <= 0)
266 begin = 1;
268 TextReader reader = new StringReader (e.FileText);
269 while ((s = reader.ReadLine ()) != null) {
270 line++;
271 if (line < begin)
272 continue;
274 if (line > end)
275 break;
277 if (beginerror == line)
278 builder.Append ("<span style=\"color: red\">");
280 builder.AppendFormat ("{0}\r\n", HtmlEncode (s));
282 if (enderror <= line) {
283 builder.Append ("</span>");
284 enderror = end + 1; // one shot
289 public int GetHttpCode ()
291 return http_code;
294 public static HttpException CreateFromLastError (string message)
296 WebTrace.WriteLine ("CreateFromLastError");
297 return new HttpException (message, 0);