예제 #1
0
        // Must load the value of the fragment and store it in the values collection
        public virtual void Init(ExceptionPageTemplateValues values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            string tmp = Value;

            if (tmp != null)
            {
                values.Add(Name, tmp);
                return;
            }

            tmp = FilePath;
            if (!String.IsNullOrEmpty(tmp))
            {
                values.Add(Name, LoadFile(tmp));
                return;
            }

            tmp = ResourceName;
            if (!String.IsNullOrEmpty(tmp))
            {
                values.Add(Name, LoadResource(tmp));
                return;
            }
        }
예제 #2
0
        void Render(ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType, Action <string> writer)
        {
            if (fragments == null || fragments.Count == 0 || values.Count == 0)
            {
                return;
            }

            InitFragments(values);
            string value;

            foreach (ExceptionPageTemplateFragment fragment in fragments)
            {
                if (fragment == null || (fragment.ValidForPageType & pageType) == 0)
                {
                    continue;
                }

                value = values.Get(fragment.Name);
                if (value == null || !fragment.Visible(values))
                {
                    continue;
                }

                writer(fragment.ReplaceMacros(value, values));
            }
        }
예제 #3
0
 void FillDefaultCustomErrorValues(ExceptionPageTemplateValues values)
 {
     values.Add(ExceptionPageTemplate.Template_PageTitleName, "Runtime Error");
     values.Add(ExceptionPageTemplate.Template_ExceptionTypeName, "Runtime Error");
     values.Add(ExceptionPageTemplate.Template_ExceptionMessageName, "A runtime error has occurred");
     values.Add(ExceptionPageTemplate.Template_DescriptionName, "An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed (for security reasons).");
     values.Add(ExceptionPageTemplate.Template_DetailsName, "To enable the details of this specific error message to be viewable, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.");
 }
예제 #4
0
		void InitFragments (ExceptionPageTemplateValues values)
		{
			foreach (ExceptionPageTemplateFragment fragment in fragments) {
				if (fragment == null)
					continue;

				fragment.Init (values);
			}
		}
예제 #5
0
		public string Render (ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
		{
			if (values == null)
				throw new ArgumentNullException ("values");
			var sb = new StringBuilder ();

			Render (values, pageType, (string text) => {
				sb.Append (text);
			});
			
			return sb.ToString ();
		}
예제 #6
0
        void InitFragments(ExceptionPageTemplateValues values)
        {
            foreach (ExceptionPageTemplateFragment fragment in fragments)
            {
                if (fragment == null)
                {
                    continue;
                }

                fragment.Init(values);
            }
        }
예제 #7
0
        public string Render(ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }
            var sb = new StringBuilder();

            Render(values, pageType, (string text) => {
                sb.Append(text);
            });

            return(sb.ToString());
        }
예제 #8
0
        public void Render(HttpResponse response, ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
        {
            if (response == null)
            {
                return;
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            Render(values, pageType, (string text) => {
                response.Write(text);
            });
        }
예제 #9
0
        public string ReplaceMacros(string value, ExceptionPageTemplateValues values)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(value);
            }

            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            List <string> macroNames = MacroNames;

            if (macroNames == null || macroNames.Count == 0)
            {
                return(value);
            }

            var    sb = new StringBuilder(value);
            string macroValue;

            foreach (string macro in macroNames)
            {
                if (String.IsNullOrEmpty(macro))
                {
                    continue;
                }

                macroValue = values.Get(macro);
                if (macroValue == null)
                {
                    macroValue = String.Empty;
                }

                sb.Replace("@" + macro + "@", macroValue);
            }

            return(sb.ToString());
        }
예제 #10
0
        public virtual bool Visible(ExceptionPageTemplateValues values)
        {
            List <string> required = RequiredMacros;

            if (required == null || required.Count == 0)
            {
                return(true);
            }

            if (values == null || values.Count == 0)
            {
                return(false);
            }

            foreach (string macro in required)
            {
                if (values.Get(macro) == null)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #11
0
		void FillDefaultErrorValues (bool showTrace, bool showExceptionType, Exception baseEx, ExceptionPageTemplateValues values)
		{
			if (baseEx == null)
				baseEx = this;
			
			values.Add (ExceptionPageTemplate.Template_PageTitleName, String.Format ("Error{0}", http_code != 0 ? " " + http_code : String.Empty));
			values.Add (ExceptionPageTemplate.Template_ExceptionTypeName, showExceptionType ? baseEx.GetType ().ToString () : "Runtime error");
			values.Add (ExceptionPageTemplate.Template_ExceptionMessageName, http_code == 404 ? "The resource cannot be found." : HtmlEncode (baseEx.Message));

			string tmp = http_code != 0 ? "HTTP " + http_code + "." : String.Empty;
			values.Add (ExceptionPageTemplate.Template_DescriptionName, tmp + (http_code == 404 ? ERROR_404_DESCRIPTION : HtmlEncode (Description)));

			if (!String.IsNullOrEmpty (resource_name))
				values.Add (ExceptionPageTemplate.Template_DetailsName, "Requested URL: " + HtmlEncode (resource_name));
			else if (http_code == 404)
				values.Add (ExceptionPageTemplate.Template_DetailsName, "No virtual path information available.");
			else if (baseEx is HttpException) {
				tmp = ((HttpException)baseEx).Description;
				values.Add (ExceptionPageTemplate.Template_DetailsName, !String.IsNullOrEmpty (tmp) ? HtmlEncode (tmp) : "Web exception occurred but no additional error description given.");
			} else {
				var sb = new StringBuilder ("Non-web exception.");

				tmp = baseEx.Source;
				if (!String.IsNullOrEmpty (tmp))
					sb.AppendFormat (" Exception origin (name of application or object): {0}.", HtmlEncode (tmp));
				tmp = baseEx.HelpLink;
				if (!String.IsNullOrEmpty (tmp))
					sb.AppendFormat (" Additional information is available at {0}", HtmlEncode (tmp));
				
				values.Add (ExceptionPageTemplate.Template_DetailsName, sb.ToString ());
			}
			
			if (showTrace) {
				string stackTrace = baseEx.StackTrace;
				if (!String.IsNullOrEmpty (stackTrace))
					values.Add (ExceptionPageTemplate.Template_StackTraceName, HtmlEncode (stackTrace));
			}
		}
예제 #12
0
        void FillDefaultErrorValues(bool showTrace, bool showExceptionType, Exception baseEx, ExceptionPageTemplateValues values)
        {
            if (baseEx == null)
            {
                baseEx = this;
            }

            values.Add(ExceptionPageTemplate.Template_PageTitleName, String.Format("Error{0}", http_code != 0 ? " " + http_code : String.Empty));
            values.Add(ExceptionPageTemplate.Template_ExceptionTypeName, showExceptionType ? baseEx.GetType().ToString() : "Runtime error");
            values.Add(ExceptionPageTemplate.Template_ExceptionMessageName, http_code == 404 ? "The resource cannot be found." : HtmlEncode(baseEx.Message));

            string tmp = http_code != 0 ? "HTTP " + http_code + "." : String.Empty;

            values.Add(ExceptionPageTemplate.Template_DescriptionName, tmp + (http_code == 404 ? ERROR_404_DESCRIPTION : HtmlEncode(Description)));

            if (!String.IsNullOrEmpty(resource_name))
            {
                values.Add(ExceptionPageTemplate.Template_DetailsName, "Requested URL: " + HtmlEncode(resource_name));
            }
            else if (http_code == 404)
            {
                values.Add(ExceptionPageTemplate.Template_DetailsName, "No virtual path information available.");
            }
            else if (baseEx is HttpException)
            {
                tmp = ((HttpException)baseEx).Description;
                values.Add(ExceptionPageTemplate.Template_DetailsName, !String.IsNullOrEmpty(tmp) ? HtmlEncode(tmp) : "Web exception occurred but no additional error description given.");
            }
            else
            {
                var sb = new StringBuilder("Non-web exception.");

                tmp = baseEx.Source;
                if (!String.IsNullOrEmpty(tmp))
                {
                    sb.AppendFormat(" Exception origin (name of application or object): {0}.", HtmlEncode(tmp));
                }
                tmp = baseEx.HelpLink;
                if (!String.IsNullOrEmpty(tmp))
                {
                    sb.AppendFormat(" Additional information is available at {0}", HtmlEncode(tmp));
                }

                values.Add(ExceptionPageTemplate.Template_DetailsName, sb.ToString());
            }

            if (showTrace)
            {
                string stackTrace = baseEx.StackTrace;
                if (!String.IsNullOrEmpty(stackTrace))
                {
                    values.Add(ExceptionPageTemplate.Template_StackTraceName, HtmlEncode(stackTrace));
                }
            }
        }
예제 #13
0
		void Render (ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType, Action <string> writer)
		{
			if (fragments == null || fragments.Count == 0 || values.Count == 0)
				return;

			InitFragments (values);
			string value;
			foreach (ExceptionPageTemplateFragment fragment in fragments) {
				if (fragment == null || (fragment.ValidForPageType & pageType) == 0)
					continue;

				value = values.Get (fragment.Name);
				if (value == null || !fragment.Visible (values))
					continue;

				writer (fragment.ReplaceMacros (value, values));
			}
		}
예제 #14
0
        void FillHtmlizedErrorValues(ExceptionPageTemplateValues values, HtmlizedException exc, ref ExceptionPageTemplateType pageType)
        {
            bool isParseException   = exc is ParseException;
            bool isCompileException = (!isParseException && exc is CompilationException);

            values.Add(ExceptionPageTemplate.Template_PageTitleName, HtmlEncode(exc.Title));
            values.Add(ExceptionPageTemplate.Template_DescriptionName, HtmlEncode(exc.Description));
            values.Add(ExceptionPageTemplate.Template_StackTraceName, HtmlEncode(exc.StackTrace));
            values.Add(ExceptionPageTemplate.Template_ExceptionTypeName, exc.GetType().ToString());
            values.Add(ExceptionPageTemplate.Template_ExceptionMessageName, HtmlEncode(exc.Message));
            values.Add(ExceptionPageTemplate.Template_DetailsName, HtmlEncode(exc.ErrorMessage));

            string origin;

            if (isParseException)
            {
                origin = "Parser";
            }
            else if (isCompileException)
            {
                origin = "Compiler";
            }
            else
            {
                origin = "Other";
            }
            values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionOriginName, origin);
            if (exc.FileText != null)
            {
                pageType |= ExceptionPageTemplateType.SourceError;
                StringBuilder shortSource = new StringBuilder();
                StringBuilder longSource;

                if (isCompileException)
                {
                    longSource = new StringBuilder();
                }
                else
                {
                    longSource = null;
                }
                FormatSource(shortSource, longSource, exc);
                values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionShortSourceName, shortSource.ToString());
                values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionLongSourceName, longSource != null ? longSource.ToString() : null);

                if (exc.SourceFile != exc.FileName)
                {
                    values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile(exc.SourceFile));
                }
                else
                {
                    values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile(exc.FileName));
                }
                if (isParseException || isCompileException)
                {
                    int[] errorLines = exc.ErrorLines;
                    int   numErrors  = errorLines != null ? errorLines.Length : 0;
                    var   lines      = new StringBuilder();
                    for (int i = 0; i < numErrors; i++)
                    {
                        if (i > 0)
                        {
                            lines.Append(", ");
                        }
                        lines.Append(errorLines [i]);
                    }
                    values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionErrorLinesName, lines.ToString());
                }
            }
            else
            {
                values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile(exc.FileName));
            }

            if (isCompileException)
            {
                CompilationException cex    = exc as CompilationException;
                StringCollection     output = cex.CompilerOutput;

                if (output != null && output.Count > 0)
                {
                    pageType |= ExceptionPageTemplateType.CompilerOutput;
                    var  sb    = new StringBuilder();
                    bool first = true;
                    foreach (string s in output)
                    {
                        sb.Append(HtmlEncode(s));
                        if (first)
                        {
                            sb.Append("<br/>");
                            first = false;
                        }
                        sb.Append("<br/>");
                    }

                    values.Add(ExceptionPageTemplate.Template_HtmlizedExceptionCompilerOutputName, sb.ToString());
                }
            }
        }
예제 #15
0
		public void Render (HttpResponse response, ExceptionPageTemplateValues values, ExceptionPageTemplateType pageType)
		{
			if (response == null)
				return;

			if (values == null)
				throw new ArgumentNullException ("values");
			
			Render (values, pageType, (string text) => {
				response.Write (text);
			});
		}
예제 #16
0
		// Must load the value of the fragment and store it in the values collection
		public virtual void Init (ExceptionPageTemplateValues values)
		{
			if (values == null)
				throw new ArgumentNullException ("values");

			string tmp = Value;
			if (tmp != null) {
				values.Add (Name, tmp);
				return;
			}

			tmp = FilePath;
			if (!String.IsNullOrEmpty (tmp)) {
				values.Add (Name, LoadFile (tmp));
				return;
			}

			tmp = ResourceName;
			if (!String.IsNullOrEmpty (tmp)) {
				values.Add (Name, LoadResource (tmp));
				return;
			}
		}
예제 #17
0
        public string GetHtmlErrorMessage()
        {
            var values = new ExceptionPageTemplateValues();
            ExceptionPageTemplate template = PageTemplate;

            try {
                values.Add(ExceptionPageTemplate.Template_RuntimeVersionInformationName, RuntimeHelpers.MonoVersion);
                values.Add(ExceptionPageTemplate.Template_AspNetVersionInformationName, Environment.Version.ToString());

                HttpContext ctx = HttpContext.Current;
                ExceptionPageTemplateType pageType = ExceptionPageTemplateType.Standard;

                if (ctx != null && ctx.IsCustomErrorEnabled)
                {
                    if (http_code != 404 && http_code != 403)
                    {
                        FillDefaultCustomErrorValues(values);
                        pageType = ExceptionPageTemplateType.CustomErrorDefault;
                    }
                    else
                    {
                        FillDefaultErrorValues(false, false, null, values);
                    }
                }
                else
                {
                    Exception ex = GetBaseException();
                    if (ex == null)
                    {
                        ex = this;
                    }

                    values.Add(ExceptionPageTemplate.Template_FullStackTraceName, FormatFullStackTrace());
                    HtmlizedException htmlException = ex as HtmlizedException;
                    if (htmlException == null)
                    {
                        FillDefaultErrorValues(true, true, ex, values);
                    }
                    else
                    {
                        pageType = ExceptionPageTemplateType.Htmlized;
                        FillHtmlizedErrorValues(values, htmlException, ref pageType);
                    }
                }

                return(template.Render(values, pageType));
            } catch (Exception ex) {
                Console.Error.WriteLine("An exception has occurred while generating HttpException page:");
                Console.Error.WriteLine(ex);
                Console.Error.WriteLine();
                Console.Error.WriteLine("The actual exception which was being reported was:");
                Console.Error.WriteLine(this);

                // we need the try/catch block in case the
                // problem was with MapPath, which will cause
                // IsCustomErrorEnabled to throw an exception
                try {
                    FillDefaultCustomErrorValues(values);
                    return(template.Render(values, ExceptionPageTemplateType.CustomErrorDefault));
                } catch {
                    return(DoubleFaultExceptionMessage);
                }
            }
        }
예제 #18
0
		public string ReplaceMacros (string value, ExceptionPageTemplateValues values)
		{
			if (String.IsNullOrEmpty (value))
				return value;

			if (values == null)
				throw new ArgumentNullException ("values");

			List <string> macroNames = MacroNames;
			if (macroNames == null || macroNames.Count == 0)
				return value;

			var sb = new StringBuilder (value);
			string macroValue;
			
			foreach (string macro in macroNames) {
				if (String.IsNullOrEmpty (macro))
					continue;

				macroValue = values.Get (macro);
				if (macroValue == null)
					macroValue = String.Empty;

				sb.Replace ("@" + macro + "@", macroValue);
			}
				
			return sb.ToString ();
		}
예제 #19
0
		public virtual bool Visible (ExceptionPageTemplateValues values)
		{
			List <string> required = RequiredMacros;
			if (required == null || required.Count == 0)
				return true;

			if (values == null || values.Count == 0)
				return false;
			
			foreach (string macro in required) {
				if (values.Get (macro) == null)
					return false;
			}

			return true;
		}
예제 #20
0
		public string GetHtmlErrorMessage ()
		{
			var values = new ExceptionPageTemplateValues ();
			ExceptionPageTemplate template = PageTemplate;

			try {
				values.Add (ExceptionPageTemplate.Template_RuntimeVersionInformationName, RuntimeHelpers.MonoVersion);
				values.Add (ExceptionPageTemplate.Template_AspNetVersionInformationName, Environment.Version.ToString ());
				
				HttpContext ctx = HttpContext.Current;
				ExceptionPageTemplateType pageType = ExceptionPageTemplateType.Standard;

				if (ctx != null && ctx.IsCustomErrorEnabled) {
					if (http_code != 404 && http_code != 403) {
						FillDefaultCustomErrorValues (values);
						pageType = ExceptionPageTemplateType.CustomErrorDefault;
					} else
						FillDefaultErrorValues (false, false, null, values);
				} else {
					Exception ex = GetBaseException ();
					if (ex == null)
						ex = this;

					values.Add (ExceptionPageTemplate.Template_FullStackTraceName, FormatFullStackTrace ());
					HtmlizedException htmlException = ex as HtmlizedException;
					if (htmlException == null)
						FillDefaultErrorValues (true, true, ex, values);
					else {
						pageType = ExceptionPageTemplateType.Htmlized;
						FillHtmlizedErrorValues (values, htmlException, ref pageType);
					}
				}
				
				return template.Render (values, pageType);
			} catch (Exception ex) {
				Console.Error.WriteLine ("An exception has occurred while generating HttpException page:");
				Console.Error.WriteLine (ex);
				Console.Error.WriteLine ();
				Console.Error.WriteLine ("The actual exception which was being reported was:");
				Console.Error.WriteLine (this);

				// we need the try/catch block in case the
				// problem was with MapPath, which will cause
				// IsCustomErrorEnabled to throw an exception
				try {
					FillDefaultCustomErrorValues (values);
					return template.Render (values, ExceptionPageTemplateType.CustomErrorDefault);
				} catch {
					return DoubleFaultExceptionMessage;
				}
			}
		}
예제 #21
0
		void FillDefaultCustomErrorValues (ExceptionPageTemplateValues values)
		{
			values.Add (ExceptionPageTemplate.Template_PageTitleName, "Runtime Error");
			values.Add (ExceptionPageTemplate.Template_ExceptionTypeName, "Runtime Error");
			values.Add (ExceptionPageTemplate.Template_ExceptionMessageName, "A runtime error has occurred");
			values.Add (ExceptionPageTemplate.Template_DescriptionName, "An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed (for security reasons).");
			values.Add (ExceptionPageTemplate.Template_DetailsName, "To enable the details of this specific error message to be viewable, please create a &lt;customErrors&gt; tag within a &quot;web.config&quot; configuration file located in the root directory of the current web application. This &lt;customErrors&gt; tag should then have its &quot;mode&quot; attribute set to &quot;Off&quot;.");
		}
예제 #22
0
		void FillHtmlizedErrorValues (ExceptionPageTemplateValues values, HtmlizedException exc, ref ExceptionPageTemplateType pageType)
		{
			bool isParseException = exc is ParseException;
			bool isCompileException = (!isParseException && exc is CompilationException);
			values.Add (ExceptionPageTemplate.Template_PageTitleName, HtmlEncode (exc.Title));
			values.Add (ExceptionPageTemplate.Template_DescriptionName, HtmlEncode (exc.Description));
			values.Add (ExceptionPageTemplate.Template_StackTraceName, HtmlEncode (exc.StackTrace));
			values.Add (ExceptionPageTemplate.Template_ExceptionTypeName, exc.GetType ().ToString ());
			values.Add (ExceptionPageTemplate.Template_ExceptionMessageName, HtmlEncode (exc.Message));
			values.Add (ExceptionPageTemplate.Template_DetailsName, HtmlEncode (exc.ErrorMessage));

			string origin;
			if (isParseException)
				origin = "Parser";
			else if (isCompileException)
				origin = "Compiler";
			else
				origin = "Other";
			values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionOriginName, origin);
			if (exc.FileText != null) {
				pageType |= ExceptionPageTemplateType.SourceError;
				StringBuilder shortSource = new StringBuilder ();
				StringBuilder longSource;
				
				if (isCompileException)
					longSource = new StringBuilder ();
				else
					longSource = null;
				FormatSource (shortSource, longSource, exc);
				values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionShortSourceName, shortSource.ToString ());
				values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionLongSourceName, longSource != null ? longSource.ToString () : null);
				
				if (exc.SourceFile != exc.FileName)
					values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile (exc.SourceFile));
				else
					values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile (exc.FileName));
				if (isParseException || isCompileException) {
					int[] errorLines = exc.ErrorLines;
					int numErrors = errorLines != null ? errorLines.Length : 0;
					var lines = new StringBuilder ();
					for (int i = 0; i < numErrors; i++) {
						if (i > 0)
							lines.Append (", ");
						lines.Append (errorLines [i]);
					}
					values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionErrorLinesName, lines.ToString ());
				}
			} else
				values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionSourceFileName, FormatSourceFile (exc.FileName));

			if (isCompileException) {
				CompilationException cex = exc as CompilationException;
				StringCollection output = cex.CompilerOutput;

				if (output != null && output.Count > 0) {
					pageType |= ExceptionPageTemplateType.CompilerOutput;
					var sb = new StringBuilder ();
					bool first = true;
					foreach (string s in output) {
						sb.Append (HtmlEncode (s));
						if (first) {
							sb.Append ("<br/>");
							first = false;
						}
						sb.Append ("<br/>");
					}
					
					values.Add (ExceptionPageTemplate.Template_HtmlizedExceptionCompilerOutputName, sb.ToString ());
				}
			}
		}