/// <summary>
 /// Override this method to handle displaying of error information.
 /// You can write to the HTTP output stream. If using MVC you can
 /// use the Westwind.Web.ViewRenderer class to easily display an
 /// MVC view.
 /// </summary>
 /// <param name="errorHandler">Contains error and request information</param>
 /// <param name="model">
 /// Model that contains a few simple properties like title and message
 /// as well as an instance of the errorHandler object passed in to allow
 /// for error pages that can provide a wealth of detail if desired.
 /// </param>
 protected virtual void  OnDisplayError(WebErrorHandler errorHandler, ErrorViewModel model)
 {
 }
        /// <summary>
        /// Semi generic Application_Error handler method that can be used
        /// to handle errors without additional code. You should implement
        /// the DisplayError event and LogError Event methods to display
        /// a message when ErrorHandlingModes is not Default
        /// </summary>
        /// <param name="errorHandlingMode"></param>
        public void HandleError(ErrorHandlingModes errorHandlingMode = ErrorHandlingModes.Default)
        {
            var context  = HttpContext.Current;
            var Response = context.Response;
            var Server   = context.Server;

            if (Response.Filter != null)
            {
                var f = Response.Filter;
                f = null;  // REQUIRED - w/o this null setting doesn't work
                Response.Filter = null;
            }

            var ex = Server.GetLastError().GetBaseException();

            OriginalHttpStatusCode = 500;
            if (ex is HttpException)
            {
                var httpException = (HttpException)ex;
                OriginalHttpStatusCode = httpException.GetHttpCode();
            }

            var errorHandler = new WebErrorHandler(ex);

            errorHandler.Parse();

            if (LogManagerConfiguration.Current.LogErrors &&
                OriginalHttpStatusCode < 400 || OriginalHttpStatusCode > 410)
            {
                OnLogError(errorHandler, LastException);
            }

            var model = new ErrorViewModel();

            model.WebErrorHandler   = this;
            model.ErrorHandlingMode = errorHandlingMode;

            if (errorHandlingMode == ErrorHandlingModes.Default)
            {
                // return default ASP.NET error screen behavior
                // Yellow Screen of Death or ASP.NET DisplayErrors form
                Response.TrySkipIisCustomErrors = true;
                return;
            }

            //if (errorHandlingMode == ErrorHandlingModes.DeveloperErrorMessage)
            //{
            //    model.Message = errorHandler.ToString();
            //    model.MessageIsHtml = true;
            //}
            //else if (errorHandlingMode == ErrorHandlingModes.ApplicationErrorMessage)
            //{
            //    // do nothing - already got our message
            //}

            Response.ClearContent();
            Response.ClearHeaders();
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;

            Response.ContentType = "text/html";
            Response.StatusCode  = 500;

            OnDisplayError(errorHandler, model);
        }
        /// <summary>
        /// Semi generic Application_Error handler method that can be used
        /// to handle errors without additional code. You should implement
        /// the DisplayError event and LogError Event methods to display
        /// a message when ErrorHandlingModes is not Default
        /// </summary>
        /// <param name="errorHandlingMode"></param>
        public void HandleError(ErrorHandlingModes errorHandlingMode = ErrorHandlingModes.Default)
        {
            var context = HttpContext.Current;
            var Response = context.Response;            
            var Server = context.Server;

            if (Response.Filter != null)
            {
                var f = Response.Filter;
                f = null;  // REQUIRED! - w/o this null setting doesn't work
                Response.Filter = null;
            }

            var ex = Server.GetLastError().GetBaseException();

            OriginalHttpStatusCode = 500;
            if (ex is HttpException)
            {
                var httpException = (HttpException)ex;
                OriginalHttpStatusCode = httpException.GetHttpCode();
            }

            var errorHandler = new WebErrorHandler(ex);
            errorHandler.RetrieveSourceLines = this.RetrieveSourceLines;
            errorHandler.Parse();

            if (LogManagerConfiguration.Current.LogErrors &&
                // don't log URL link errors
                OriginalHttpStatusCode < 400 || OriginalHttpStatusCode > 410)
            {                
                OnLogError(errorHandler, LastException);
            }

            var model = new ErrorViewModel();
            model.Title = Resources.AnErrorOccurredInYourApplication;
            model.Message = errorHandler.ErrorMessage;
            
            model.WebErrorHandler = errorHandler;
            model.ErrorHandlingMode = errorHandlingMode;            

            if (errorHandlingMode == ErrorHandlingModes.Default)
            {
                 // return default ASP.NET error screen behavior
                // Yellow Screen of Death or ASP.NET DisplayErrors form
                Response.TrySkipIisCustomErrors = true;
                return;
            }        

            Response.ClearContent();
            Response.ClearHeaders();
            Server.ClearError();
            Response.TrySkipIisCustomErrors = true;

            Response.ContentType = "text/html";
            Response.StatusCode = 500;

            OnDisplayError(errorHandler, model);
        }
 /// <summary>
 /// Allows overriding of DisplayError
 /// </summary>
 /// <param name="error"></param>
 /// <param name="model"></param>
 protected virtual void OnDisplayError(WebErrorHandler error, ErrorViewModel model)
 {
     if (DisplayError != null)
         DisplayError(error, model);
 }
 /// <summary>
 /// Override this method to handle displaying of error information.
 /// You can write to the HTTP output stream. If using MVC you can
 /// use the Westwind.Web.ViewRenderer class to easily display an
 /// MVC view.
 /// </summary>
 /// <param name="errorHandler">Contains error and request information</param>
 /// <param name="model">
 /// Model that contains a few simple properties like title and message
 /// as well as an instance of the errorHandler object passed in to allow
 /// for error pages that can provide a wealth of detail if desired.
 /// </param>
 protected virtual void  OnDisplayError(WebErrorHandler errorHandler, ErrorViewModel model)
 {            
 }