// // 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) // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System.IO; using System.Runtime.Serialization; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Text; using System.Web.Util; using System.Web.Compilation; using System.Web.Management; using System.Collections.Specialized; namespace System.Web { // CAS [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] [Serializable] public class HttpException : ExternalException { const string DEFAULT_DESCRIPTION_TEXT = "Error processing request."; const string ERROR_404_DESCRIPTION = "The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly."; int webEventCode = WebEventCodes.UndefinedEventCode; int http_code = 500; string resource_name; string description; const string errorStyleFonts = "\"Verdana\",\"DejaVu Sans\",sans-serif"; #if NET_4_0 public #else internal #endif int WebEventCode { get { return webEventCode; } } public HttpException () { } public HttpException (string message) : base (message) { } public HttpException (string message, Exception innerException) : base (message, innerException) { } public HttpException (int httpCode, string message) : base (message) { http_code = httpCode; } internal HttpException (int httpCode, string message, string resourceName) : this (httpCode, message) { resource_name = resourceName; } internal HttpException (int httpCode, string message, string resourceName, string description) : this (httpCode, message, resourceName) { this.description = description; } protected HttpException (SerializationInfo info, StreamingContext context) : base (info, context) { http_code = info.GetInt32 ("_httpCode"); webEventCode = info.GetInt32 ("_webEventCode"); } [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData (SerializationInfo info, StreamingContext context) { base.GetObjectData (info, context); info.AddValue ("_httpCode", http_code); info.AddValue ("_webEventCode", webEventCode); } public HttpException (int httpCode, string message, int hr) : base (message, hr) { http_code = httpCode; } public HttpException (string message, int hr) : base (message, hr) { } public HttpException (int httpCode, string message, Exception innerException) : base (message, innerException) { http_code = httpCode; } internal HttpException (int httpCode, string message, Exception innerException, string resourceName) : this (httpCode, message, innerException) { resource_name = resourceName; } public string GetHtmlErrorMessage () { try { HttpContext ctx = HttpContext.Current; if (ctx != null && ctx.IsCustomErrorEnabled) { if (http_code != 404 && http_code != 403) return GetCustomErrorDefaultMessage (); else return GetDefaultErrorMessage (false, null); } Exception ex = GetBaseException (); if (ex == null) ex = this; HtmlizedException htmlException = ex as HtmlizedException; if (htmlException == null) return GetDefaultErrorMessage (true, ex); return GetHtmlizedErrorMessage (htmlException); } catch (Exception ex) { Console.WriteLine (ex); // we need the try/catch block in case the // problem was with MapPath, which will cause // IsCustomErrorEnabled to throw an exception return GetCustomErrorDefaultMessage (); } } internal virtual string Description { get { if (description != null) return description; return DEFAULT_DESCRIPTION_TEXT; } set { if (value != null && value.Length > 0) description = value; else description = DEFAULT_DESCRIPTION_TEXT; } } internal static HttpException NewWithCode (string message, int webEventCode) { var ret = new HttpException (message); ret.SetWebEventCode (webEventCode); return ret; } internal static HttpException NewWithCode (string message, Exception innerException, int webEventCode) { var ret = new HttpException (message, innerException); ret.SetWebEventCode (webEventCode); return ret; } internal static HttpException NewWithCode (int httpCode, string message, int webEventCode) { var ret = new HttpException (httpCode, message); ret.SetWebEventCode (webEventCode); return ret; } internal static HttpException NewWithCode (int httpCode, string message, Exception innerException, string resourceName, int webEventCode) { var ret = new HttpException (httpCode, message, innerException, resourceName); ret.SetWebEventCode (webEventCode); return ret; } internal static HttpException NewWithCode (int httpCode, string message, string resourceName, int webEventCode) { var ret = new HttpException (httpCode, message, resourceName); ret.SetWebEventCode (webEventCode); return ret; } internal static HttpException NewWithCode (int httpCode, string message, Exception innerException, int webEventCode) { var ret = new HttpException (httpCode, message, innerException); ret.SetWebEventCode (webEventCode); return ret; } internal void SetWebEventCode (int webEventCode) { this.webEventCode = webEventCode; } void WriteFileTop (StringBuilder builder, string title) { #if TARGET_J2EE builder.AppendFormat ("