/// <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>
        /// 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 for logging error information in a subclass
 /// </summary>
 /// <param name="error"></param>
 /// <param name="ex"></param>
 protected virtual void OnLogError(WebErrorHandler error, Exception ex)
 {
     if (LogError != null)
     {
         LogError(error, ex);
     }
 }
        void context_Error(object sender, EventArgs e)
        {
            var handler = new WebErrorHandler();

            handler.LogError     += OnLogError;
            handler.DisplayError += OnDisplayError;

            handler.HandleError(ErrorHandlingMode);
        }
        void context_Error(object sender, EventArgs e)
        {
            var handler = new WebErrorHandler();

            handler.LogError += OnLogError;
            handler.DisplayError += OnDisplayError;

            handler.HandleError(ErrorHandlingMode);
        }
        public string ErrorHandler()
        {
            try
            {
                string x = null;
                x.ToString();
            }
            catch (Exception ex)
            {
                var eh = new WebErrorHandler(ex);
                return eh.ToString();
            }

            return null;
        }
 /// <summary>
 /// Override this method to handle logging of errors. Gets passed
 /// the WebErrorHandler instance that is fully parsed and filled
 /// with the error and Http request data.
 /// </summary>
 /// <param name="errorHandler">Contains formatted error and request information</param>
 /// <param name="ex">The original exception that caused the error</param>
 protected virtual void OnLogError(WebErrorHandler errorHandler, Exception ex)
 {
 }
 /// <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>
 /// 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>
        /// Allows overriding for logging error information in a subclass
        /// </summary>
        /// <param name="error"></param>
        /// <param name="ex"></param>
	    protected virtual void OnLogError(WebErrorHandler error, Exception ex)
	    {
	        if (LogError != null)
	            LogError(error, ex);
	    }
        /// <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>
 /// Override this method to handle logging of errors. Gets passed
 /// the WebErrorHandler instance that is fully parsed and filled
 /// with the error and Http request data.
 /// </summary>
 /// <param name="errorHandler">Contains formatted error and request information</param>
 /// <param name="ex">The original exception that caused the error</param>
 protected virtual void OnLogError(WebErrorHandler errorHandler, Exception ex)
 {               
 }
 /// <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)
 {            
 }
Example #15
0
        protected void Application_Error()
        {
            try
            {
                Exception serverException = Server.GetLastError();

                WebErrorHandler errorHandler;

                // Try to log the inner Exception since that's what
                // contains the 'real' error.
                if (serverException.InnerException != null)
                    serverException = serverException.InnerException;

                errorHandler = new WebErrorHandler(serverException);
                
                // MUST clear out any previous response in case error occurred in a view
                // that already started rendering
                Response.Clear();


                // Log the error if specified
                if (LogManagerConfiguration.Current.LogErrors)
                {
                    errorHandler.Parse();

                    //try
                    //{
                        WebLogEntry entry = new WebLogEntry(serverException, this.Context);
                        entry.Details = errorHandler.ToString();

                        LogManager.Current.WriteEntry(entry);
                    //}
                    //catch {  /* Log Error shouldn't kill the error handler */ }
                }

                // Retrieve the detailed String information of the Error
                string ErrorDetail = errorHandler.ToString();

                // Optionally email it to the Admin contacts set up in WebStoreConfig
                if (!string.IsNullOrEmpty(App.Configuration.ErrorEmailAddress))
                    AppWebUtils.SendAdminEmail(App.Configuration.ApplicationTitle + "Error: " + Request.RawUrl, ErrorDetail, "", 
                                               App.Configuration.ErrorEmailAddress);


                // Debug modes handle different error display mechanisms
                // Default  - default ASP.Net - depends on web.config settings
                // Developer  - display a generic application error message with no error info
                // User  - display a detailed error message with full error info independent of web.config setting
                if (App.Configuration.DebugMode == DebugModes.DeveloperErrorMessage)
                {

                    Server.ClearError();
                    Response.TrySkipIisCustomErrors = true;
                    ErrorController.ShowErrorPage("Application Error", "<div class='codedisplay'><pre id='Code'>" + HttpUtility.HtmlEncode(ErrorDetail) + "</pre></div>");
                    return;
                }

                else if (App.Configuration.DebugMode == DebugModes.ApplicationErrorMessage)
                {
                    string StockMessage =
                            "The Server Administrator has been notified and the error logged.<p>" +
                            "Please continue by either clicking the back button or by returning to the home page.</p>" +
                            "<p><b><a href='" + Request.ApplicationPath + "'>Click here to continue...</a></b></p>";

                    // Handle some stock errors that may require special error pages
                    HttpException httpException = serverException as HttpException;
                    if (httpException != null)
                    {
                        int HttpCode = httpException.GetHttpCode();
                        Server.ClearError();

                        if (HttpCode == 404) // Page Not Found 
                        {
                            Response.StatusCode = 404;
                            ErrorController.ShowErrorPage("Page not found",
                                "You've accessed an invalid page on this Web server. " +
                                StockMessage,null);
                            return;
                        }
                        if (HttpCode == 401) // Access Denied 
                        {
                            Response.StatusCode = 401;
                            ErrorController.ShowErrorPage("Access Denied",
                                "You've accessed a resource that requires a valid login. " +
                                StockMessage);
                            return;
                        }
                    }

                    // Display a generic error message
                    Server.ClearError();
                    Response.StatusCode = 500;

                    Response.TrySkipIisCustomErrors = true;
                    
                    ErrorController.ShowErrorPage("Application Error",
                        "We're sorry, but an unhandled error occurred on the server. " +
                        StockMessage);

                    return;
                }

                return;
            }
            catch (Exception ex)
            {
                // Failure in the attempt to report failure - try to email
                if (!string.IsNullOrEmpty(App.Configuration.ErrorEmailAddress))
                {
                    AppWebUtils.SendAdminEmail(App.Configuration.ApplicationTitle + "Error: " + Request.RawUrl,
                            "Application_Error failed!\r\n\r\n" +
                            ex.ToString(), "",App.Configuration.ErrorEmailAddress);
                }

                // and display an error message
                Server.ClearError();
                Response.StatusCode = 500;
                Response.TrySkipIisCustomErrors = true;
                
                ErrorController.ShowErrorPage("Application Error Handler Failed",
                        "The application Error Handler failed with an exception." +
                        (App.Configuration.DebugMode == DebugModes.DeveloperErrorMessage ? "<pre>" + ex.ToString() + "</pre>" : ""),null);

            }
        }