private void WriteErrorMessage(Exception e, bool dontShowSensitiveErrors)
 {
     ErrorFormatter errorFormatter = null;
     CultureInfo dynamicUICulture = null;
     CultureInfo currentUICulture = null;
     bool flag = false;
     if (this._context.DynamicUICulture != null)
     {
         dynamicUICulture = this._context.DynamicUICulture;
     }
     else
     {
         GlobalizationSection globalization = RuntimeConfig.GetLKGConfig(this._context).Globalization;
         if ((globalization != null) && !string.IsNullOrEmpty(globalization.UICulture))
         {
             try
             {
                 dynamicUICulture = HttpServerUtility.CreateReadOnlyCultureInfo(globalization.UICulture);
             }
             catch
             {
             }
         }
     }
     this.GenerateResponseHeadersForHandler();
     if (dynamicUICulture != null)
     {
         currentUICulture = Thread.CurrentThread.CurrentUICulture;
         Thread.CurrentThread.CurrentUICulture = dynamicUICulture;
         flag = true;
     }
     try
     {
         try
         {
             errorFormatter = this.GetErrorFormatter(e);
             if (dontShowSensitiveErrors && !errorFormatter.CanBeShownToAllUsers)
             {
                 errorFormatter = new GenericApplicationErrorFormatter(this.Request.IsLocal);
             }
             if (ErrorFormatter.RequiresAdaptiveErrorReporting(this.Context))
             {
                 this._writer.Write(errorFormatter.GetAdaptiveErrorMessage(this.Context, dontShowSensitiveErrors));
             }
             else
             {
                 this._writer.Write(errorFormatter.GetHtmlErrorMessage(dontShowSensitiveErrors));
                 if (!dontShowSensitiveErrors && HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Medium))
                 {
                     this._writer.Write("<!-- \r\n");
                     this.WriteExceptionStack(e);
                     this._writer.Write("-->");
                 }
                 if (!dontShowSensitiveErrors && !this.Request.IsLocal)
                 {
                     this._writer.Write("<!-- \r\n");
                     this._writer.Write(System.Web.SR.GetString("Information_Disclosure_Warning"));
                     this._writer.Write("-->");
                 }
             }
             if (this._closeConnectionAfterError)
             {
                 this.Flush();
                 this.Close();
             }
         }
         finally
         {
             if (flag)
             {
                 Thread.CurrentThread.CurrentUICulture = currentUICulture;
             }
         }
     }
     catch
     {
         throw;
     }
 }
        private void WriteErrorMessage(Exception e, bool dontShowSensitiveErrors) {
            ErrorFormatter errorFormatter = null;
            CultureInfo uiculture = null, savedUiculture = null;
            bool needToRestoreUiculture = false;

            if (_context.DynamicUICulture != null) {
                // if the user set the culture dynamically use it
                uiculture =  _context.DynamicUICulture;
            }
            else  {
                // get the UI culture under which the error text must be created (use LKG to avoid errors while reporting error)
                GlobalizationSection globConfig = RuntimeConfig.GetLKGConfig(_context).Globalization;
                if ((globConfig != null) && (!String.IsNullOrEmpty(globConfig.UICulture))) {
                    try {
                        uiculture = HttpServerUtility.CreateReadOnlyCultureInfo(globConfig.UICulture);
                    }
                    catch {
                    }
                }
            }

            //  In Integrated mode, generate the necessary response headers for the error
            GenerateResponseHeadersForHandler();

            // set the UI culture
            if (uiculture != null) {
                savedUiculture = Thread.CurrentThread.CurrentUICulture;
                Thread.CurrentThread.CurrentUICulture = uiculture;
                needToRestoreUiculture = true;
            }

            try {
                try {
                    // Try to get an error formatter
                    errorFormatter = GetErrorFormatter(e);
#if DBG
                    Debug.Trace("internal", "Error stack for " + Request.Path, e);
#endif
                    if (dontShowSensitiveErrors && !errorFormatter.CanBeShownToAllUsers)
                        errorFormatter = new GenericApplicationErrorFormatter(Request.IsLocal);

                    Debug.Trace("internal", "errorFormatter's type = " +  errorFormatter.GetType());

                    if (ErrorFormatter.RequiresAdaptiveErrorReporting(Context)) {
                        _writer.Write(errorFormatter.GetAdaptiveErrorMessage(Context, dontShowSensitiveErrors));
                    }
                    else {
                        _writer.Write(errorFormatter.GetHtmlErrorMessage(dontShowSensitiveErrors));

                        // Write a stack dump in an HTML comment for debugging purposes
                        // Only show it for Asp permission medium or higher (ASURT 126373)
                        if (!dontShowSensitiveErrors &&
                            HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Medium)) {
                            _writer.Write("<!-- \r\n");
                            WriteExceptionStack(e);
                            _writer.Write("-->");
                        }
                         if (!dontShowSensitiveErrors && !Request.IsLocal ) {
                             _writer.Write("<!-- \r\n");
                             _writer.Write(SR.GetString(SR.Information_Disclosure_Warning));
                             _writer.Write("-->");
                         }
                    }

                    if (_closeConnectionAfterError) {
                        Flush();
                        Close();
                    }
                }
                finally {
                    // restore ui culture
                    if (needToRestoreUiculture)
                        Thread.CurrentThread.CurrentUICulture = savedUiculture;
                }
            }
            catch { // Protect against exception filters
                throw;
            }
        }