Ejemplo n.º 1
0
        //TRATATIAS DE ERROS
        protected void Application_Error(object sender, EventArgs e)
        {
            var app     = (MvcApplication)sender;
            var context = app.Context;
            var ex      = app.Server.GetLastError();

            context.Response.Clear();
            context.ClearError();

            HttpException httpException = ExceptionMethod(ex);

            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["exception"]  = ex;
            routeData.Values["action"]     = "DefaultError";

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                case 404:
                    routeData.Values["action"] = "NotFound404";
                    break;

                case 500:
                    routeData.Values["action"] = "ServerError500";
                    break;
                }
            }

            IController controller = new ErrorsController();

            controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData));
        }
Ejemplo n.º 2
0
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();

            routeData.Values["controller"] = "Error";
            routeData.Values["action"]     = "Http404";
            //routeData.Values["exception"] = exception;
            Response.StatusCode = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                case 403:
                    routeData.Values["action"] = "Http404";
                    break;

                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
                }
            }
            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController        errorsController = new ErrorsController();
            HttpContextWrapper wrapper          = new HttpContextWrapper(Context);
            var rc = new RequestContext(wrapper, routeData);

            errorsController.Execute(rc);
        }
Ejemplo n.º 3
0
        public void HandleError(Exception exception)
        {
            _httpContext.Response.Clear();
            _httpContext.Server.ClearError();

            if (!IfAjaxRequest())
            {
                RouteData routeData = new RouteData();
                routeData.Values["controller"] = "Errors";
                _httpContext.Response.Clear();

                routeData.Values["action"] = "Error";
                var httpException = exception as HttpException;

                if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
                {
                    routeData.Values["result"] = "404";
                }
                else
                {
                    routeData.Values["result"] = "500";
                }

                IController errorController = new ErrorsController();
                errorController.Execute(new RequestContext(new HttpContextWrapper(_httpContext), routeData));
            }
            else
            {
                _httpContext.Response.Write("error");
                _httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                _httpContext.Response.End();
            }
        }
Ejemplo n.º 4
0
        protected void Application_EndRequest()
        {
            bool error = false;
            var  rd    = new RouteData();

            rd.DataTokens["area"]   = "Admin"; // In case controller is in another area
            rd.Values["controller"] = "Errors";
            switch (Context.Response.StatusCode)
            {
            case 404:
                rd.Values["action"] = "NotFound";
                error = true;
                break;

            case 500:
                rd.Values["action"] = "ServerError";
                error = true;
                break;
            }
            if (error)
            {
                Response.Clear();
                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
        }
Ejemplo n.º 5
0
        // From http://stackoverflow.com/a/9026907
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 400)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.DataTokens["area"]   = "";
                rd.Values["controller"] = "Errors";
                rd.Values["action"]     = "BadRequest";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
            else if (Context.Response.StatusCode == 403)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.DataTokens["area"]   = "";
                rd.Values["controller"] = "Errors";
                rd.Values["action"]     = "NotAuthorized";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
            else if (Context.Response.StatusCode == 404)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.DataTokens["area"]   = "";
                rd.Values["controller"] = "Errors";
                rd.Values["action"]     = "NotFound";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
            else if (Context.Response.StatusCode == 500)
            {
                if (!Debugger.IsAttached)
                {
                    Response.Clear();

                    var rd = new RouteData();
                    rd.DataTokens["area"]   = "";
                    rd.Values["controller"] = "Errors";
                    rd.Values["action"]     = "InternalServerError";

                    IController c = new ErrorsController();
                    c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
                }
            }
        }
Ejemplo n.º 6
0
        void Application_Error(object sender, EventArgs e)
        {
            //try-catch added to aviod process on process unnecessary exception
            try
            {
                var exception     = Server.GetLastError();
                var httpException = exception as HttpException;
                Response.Clear();
                Server.ClearError();
                var routeData = new RouteData();
                routeData.Values["controller"] = "Errors";
                routeData.Values["action"]     = "General";
                routeData.Values["exception"]  = exception;

                //Response.StatusCode = 500;
                if (httpException != null)
                {
                    Response.StatusCode             = httpException.GetHttpCode();
                    Response.TrySkipIisCustomErrors = true;
                    switch (Response.StatusCode)
                    {
                    case 500:
                    {
                        routeData.Values["action"]    = "Http500";
                        routeData.Values["exception"] = exception;
                        break;
                    }

                    case 403:
                    {
                        routeData.Values["action"]    = "Http403";
                        routeData.Values["exception"] = exception;
                        break;
                    }

                    case 404:
                    {
                        routeData.Values["action"]    = "Http404";
                        routeData.Values["exception"] = exception;
                        break;
                    }
                    }
                }

                IController errorsController = new ErrorsController();
                var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                errorsController.Execute(rc);
            }
            catch (Exception)
            {
                //  throw;
            }
        }
Ejemplo n.º 7
0
        protected void Application_Error(object sender, EventArgs e)
        {
            Response.Clear();

            var rd = new RouteData();

            rd.DataTokens["area"]   = "AreaName"; // In case controller is in another area
            rd.Values["controller"] = "Errors";
            rd.Values["action"]     = "ExceptionError";
            IController c = new ErrorsController();

            c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
        }
Ejemplo n.º 8
0
        protected void Application_Error(object sender, EventArgs e)
        {
            // 發生未處理錯誤時執行的程式碼

            var app = (MvcApplication)sender;
            var ex  = app.Server.GetLastError();

            var context = app.Context;

            context.Response.Clear();
            context.ClearError();

            var httpException = ex as HttpException;

            if (httpException == null)
            {
                httpException = new HttpException(null, ex);
            }

            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = "Index";

            routeData.Values["exception"] = ex;
            routeData.Values["from_Application_Error_Event"] = true;

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                case 404:
                    routeData.Values["action"] = "PageNotFound";
                    break;

                default:
                    routeData.Values["action"] = "Index";
                    break;
                }
            }

            // Pass exception details to the target error View.
            routeData.Values.Add("error", ex.Message);

            // Avoid IIS7 getting in the middle
            context.Response.TrySkipIisCustomErrors = true;
            IController controller = new ErrorsController();

            controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData));
        }
Ejemplo n.º 9
0
        public void Execute()
        {
            if (_context.Response.StatusCode == 404)
            {
                _response.Clear();

                var rd = new RouteData();
                rd.Values["controller"] = "Errors";
                rd.Values["action"]     = "NotFound";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(_context, rd));
            }
        }
Ejemplo n.º 10
0
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.Values["controller"] = "Errors";
                rd.Values["action"]     = "NotFound404";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
        }
Ejemplo n.º 11
0
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();

                var rd = new RouteData();
                rd.DataTokens["area"]   = "AreaName"; // In case controller is in another area
                rd.Values["controller"] = "Welcome";
                rd.Values["action"]     = "LoginUser";

                IController c = new ErrorsController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
        }
    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            Response.Clear();

            var routedata = new RouteData();
            routedata.DataTokens["area"]   = "ErrorArea";   // In case controller is in another area
            routedata.Values["controller"] = "Errors";
            routedata.Values["action"]     = "NotFound";

            IController c = new ErrorsController();
            c.Execute(new RequestContext(new HttpContextWrapper(Context), routedata));
        }
    }
Ejemplo n.º 13
0
        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception     = Server.GetLastError();
            var       httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = "General";
            routeData.Values["exception"]  = exception;
            Response.StatusCode            = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                case 400:
                    routeData.Values["action"] = "Http400";
                    break;

                case 403:
                    routeData.Values["action"] = "Http403";
                    break;

                case 404:
                    routeData.Values["action"] = "Http404";
                    break;

                case 500:
                    routeData.Values["action"] = "Http500";
                    break;
                }
            }
            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController errorsController = new ErrorsController();
            var         wrapper          = new HttpContextWrapper(Context);
            var         rc = new RequestContext(wrapper, routeData);

            // Log to file (Debug Level) when http statuscode 500 (Internal Server Error)
            if (Response.StatusCode == 500)
            {
                DyntaxaLogger.WriteDebugException(exception);
            }
            errorsController.Execute(rc);
        }
Ejemplo n.º 14
0
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();

            if (ex != null)
            {
                StringBuilder err = new StringBuilder();
                err.Append("Error caught in Application_Error event\n");
                err.Append("Error in: " + (Context.Session == null ? string.Empty : Request.Url.ToString()));
                err.Append("\nError Message:" + ex.Message);
                if (null != ex.InnerException)
                {
                    err.Append("\nInner Error Message:" + ex.InnerException.Message);
                }
                err.Append("\n\nStack Trace:" + ex.StackTrace);
                Server.ClearError();

                if (null != Context.Session)
                {
                    err.Append($"Session: Identity name:[{Thread.CurrentPrincipal.Identity.Name}] IsAuthenticated:{Thread.CurrentPrincipal.Identity.IsAuthenticated}");
                }
                _log.Error(err.ToString());

                if (null != Context.Session)
                {
                    var routeData = new RouteData();
                    routeData.Values.Add("controller", "Errors");
                    routeData.Values.Add("action", "Error");
                    routeData.Values.Add("exception", ex);

                    if (ex.GetType() == typeof(HttpException))
                    {
                        routeData.Values.Add("statusCode", ((HttpException)ex).GetHttpCode());
                    }
                    else
                    {
                        routeData.Values.Add("statusCode", 500);
                    }
                    Response.TrySkipIisCustomErrors = true;
                    IController controller = new ErrorsController();
                    controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
                    Response.End();
                }
            }
        }
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();

            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = "General";
            routeData.Values["exception"]  = exception;
            Response.StatusCode            = 500;

            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();

                switch (Response.StatusCode)
                {
                case 400:
                    routeData.Values["action"] = "Http400";
                    break;

                case 403:
                    routeData.Values["action"] = "Http403";
                    break;

                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
                }
            }

            Session["ErrorException"] = exception;

            IController errorsController = new ErrorsController();

            var rc = new RequestContext(new HttpContextWrapper(Context), routeData);

            errorsController.Execute(rc);
        }
Ejemplo n.º 16
0
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = "General";
            routeData.Values["exception"]  = exception;
            Response.StatusCode            = 500;
            Response.ContentType           = "text/html"; // needed to render views rather than text response
            if (httpException != null)
            {
                // referring url
                var url = HttpContext.Current.Request.Url;
                Response.StatusCode = httpException.GetHttpCode();
                System.Diagnostics.Debug.WriteLine("Status Code: " + Response.StatusCode + " for " + url);
                switch (Response.StatusCode)
                {
                case 403:
                    routeData.Values["action"] = "Http403";
                    break;

                case 404:
                    routeData.Values["action"] = "Http404";
                    break;

                case 500:
                    routeData.Values["action"] = "Http500";
                    break;
                }
            }

            IController errorsController = new ErrorsController();
            var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);

            errorsController.Execute(rc);
        }
Ejemplo n.º 17
0
        private void ShowCustomErrorPage(Exception exception)
        {
            var httpException = exception as HttpException ?? new HttpException(500, "Internal Server Error", exception);

            Response.Clear();
            var routeData = new RouteData();

            routeData.Values.Add("controller", "Errors");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
            //Remove incase of securtity
            case 403:
                routeData.Values.Add("action", "HttpError403");
                //routeData.Values.Add("error", httpException.Message);
                break;

            case 404:
                routeData.Values.Add("action", "HttpError404");
                //routeData.Values.Add("error", httpException.Message);
                break;

            case 500:
                routeData.Values.Add("action", "HttpError500");
                //routeData.Values.Add("error", httpException.Message);
                break;

            default:
                routeData.Values.Add("action", "GeneralError");
                //routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                break;
            }

            Server.ClearError();

            IController controller = new ErrorsController();

            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
Ejemplo n.º 18
0
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            if (Context.Response.StatusCode != (int)HttpStatusCode.NotFound)
            {
                return;
            }

            // Handle 404s by executing another controller action rather than turning on customErrors so developers still see
            // the "Yellow Screen of Death" for errors other that 404s, but we still get the same "Not Implemented" custom 404
            // page as the Java training code
            Response.Clear();

            var rd = new RouteData();

            rd.DataTokens["area"]   = string.Empty;
            rd.Values["controller"] = "Errors";
            rd.Values["action"]     = "NotFound";

            IController c = new ErrorsController();

            c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
        }
Ejemplo n.º 19
0
        protected void Application_Error()
        {
            if (!HttpContext.Current.IsCustomErrorEnabled)
            {
                return;
            }
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = "General";
            routeData.Values["exception"]  = exception;
            Response.StatusCode            = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                case 403:
                    routeData.Values["action"] = "Http403";
                    break;

                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
                }
            }
            Response.TrySkipIisCustomErrors = true;
            IController errorsController = new ErrorsController();
            var         wrapper          = new HttpContextWrapper(Context);
            var         requestContext   = new RequestContext(wrapper, routeData);

            errorsController.Execute(requestContext);
        }
Ejemplo n.º 20
0
        protected override void OnError(HttpContext context, EventArgs e)
        {
            // this method has been known to be called a second time for the same request if conditions are just right (or wrong rather)
            //   such as if some handler somewhere in the chain clears the server error
            //   in which case AllErrors will have a second exception
            if (context.AllErrors != null && context.AllErrors.Length > 1)
            {
                return;
            }

            var exception     = context.Error;
            var httpException = exception as HttpException;
            var httpErrorCode = httpException == null ? (int?)null : httpException.GetHttpCode();

            // Send user to 404 page.
            context.Response.Clear();
            context.Server.ClearError();
            try
            {
                context.Response.StatusCode = httpErrorCode ?? 500;
            }
            catch (Exception ex)
            {
                Logger.Info(ex, "Cannot set status.");
            }
            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"]     = httpErrorCode == 404 ? "Http404" : "Http500";
            routeData.Values["exception"]  = exception;

            context.Response.TrySkipIisCustomErrors = true;
            IController errorsController = new ErrorsController(Jungo.Infrastructure.DependencyResolver.Current.Get <ILinkGenerator>());
            var         wrapper          = new HttpContextWrapper(context);
            var         rc = new RequestContext(wrapper, routeData);

            errorsController.Execute(rc);
        }
Ejemplo n.º 21
0
        void MvcApplication_Error(object sender, System.EventArgs e)
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;

            bool success = RaiseErrorSignal(exception);

            Response.Clear();

            var routeData = new RouteData();

            routeData.Values["controller"] = "Errors";
            routeData.Values["action"] = "InternalServerError";
            routeData.Values["exception"] = exception;

            Response.StatusCode = 500;

            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();

                switch (Response.StatusCode)
                {
                    case 403:
                        routeData.Values["action"] = eAction.Forbidden;
                        break;
                    case 404:
                        routeData.Values["action"] = eAction.NotFound;
                        break;
                    case 401:
                        routeData.Values["action"] = eAction.Unauthorized;
                        break;
                }
            }

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

            var errorsController = new ErrorsController() as IController;
            var wrapper = new HttpContextWrapper(Context);
            wrapper.Response.ContentEncoding = Encoding.UTF8;
            var requestContext = new RequestContext(wrapper, routeData);
            errorsController.Execute(requestContext);
        }
Ejemplo n.º 22
0
        protected void Application_Error(Object sender, EventArgs e)
        {
            // See http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4
            // for additional context on use of this technique

            var exception = Server.GetLastError();

            if (exception != null)
            {
                Response.Clear();
                Server.ClearError();
                var routeData = new RouteData();
                routeData.Values["controller"] = "Errors";
                routeData.Values["action"]     = "Index";
                routeData.Values["exception"]  = exception;
                Response.StatusCode            = 500;

                if (exception is HttpException)
                {
                    routeData.Values["action"] = "NotFound";
                }
                else if (exception is ContentNotFoundException || exception is UserNotFoundException)
                {
                    routeData.Values["action"] = "NotFound";
                }
                else if (exception is NotAuthorizedException)
                {
                    routeData.Values["action"] = "NotAuthorized";
                }
                else
                {
                }

                routeData.Values["errorMessage"]    = exception.Message;
                routeData.Values["errorStackTrace"] = Uri.EscapeDataString(exception.StackTrace);

                Response.TrySkipIisCustomErrors = true;
                IController errorsController = new ErrorsController();
                var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);

                /* This will run specific action without redirecting */
                errorsController.Execute(rc);
            }

            // This will invoke our error page, passing the exception message via querystring parameter
            // Note that we chose to use Server.TransferRequest, which is only supported in IIS 7 and above.
            // As an alternative, Response.Redirect could be used instead.
            // Server.Transfer does not work (see https://support.microsoft.com/en-us/kb/320439 )

            //if (exception is HttpException)
            //    {
            //        Server.TransferRequest("/Errors/NotFound", false);
            //    }
            //    else if (exception is ContentNotFoundException || exception is UserNotFoundException)
            //    {
            //        Server.TransferRequest("/Errors/NotFound?errorMessage=" + exception.Message + "&errorStackTrace=" + Uri.EscapeDataString(exception.StackTrace), false);
            //    }
            //    else if (exception is NotAuthorizedException)
            //    {
            //        Server.TransferRequest("/Errors/NotAuthorized?errorMessage=" + exception.Message + "&errorStackTrace=" + Uri.EscapeDataString(exception.StackTrace), false);
            //    }
            //    else if (exception is Exception)
            //    {
            //        Server.TransferRequest("/Errors/Index?errorMessage=" + exception.Message + "&errorStackTrace=" + Uri.EscapeDataString(exception.StackTrace), false);
            //    }

            //    // This is to stop a problem where we were seeing "gibberish" in the
            //    // chrome and firefox browsers
            //    HttpApplication app = sender as HttpApplication;
            //    app.Response.Filter = null;
        }
Ejemplo n.º 23
0
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            string Message = exception.Message;
            string Details = String.Format("Source: {0}\r\nStackTrace: {1}", exception.Source, exception.StackTrace);

            if (exception.InnerException != null)
            {
                Message += " " + exception.InnerException.Message;
                Details += String.Format("\r\nSource: {0}\r\nStackTrace: {1}", exception.InnerException.Source, exception.InnerException.StackTrace);
            }


            try
            {
                AuditEvent.AppEventError(AppSession.Parameters.GeneralAdminEmail.Value, "Unhandled exception: " + Message, AuditEvent.GetSessionDetails(Details));
            }
            catch
            {
            }

            if (HttpContext.Current.IsDebuggingEnabled || (User.Identity.IsAuthenticated && AppSession.IsMemberInAdminRole))
            {
            }
            else
            {
                Response.Clear();
                Server.ClearError();

                var routeData = new RouteData();
                routeData.Values["controller"] = "Errors";
                routeData.Values["action"]     = "General";
                routeData.Values["exception"]  = exception;
                Response.StatusCode            = 500;

                if (httpException != null)
                {
                    Response.StatusCode = httpException.GetHttpCode();
                    switch (Response.StatusCode)
                    {
                    case 403:
                        routeData.Values["action"] = "Http403";
                        break;

                    case 404:
                        routeData.Values["action"] = "Http404";
                        break;
                    }
                }

                // Avoid IIS7 getting in the middle
                // http://blog.davebouwman.com/2011/04/21/custom-404-pages-for-asp-net-mvc-3/
                Response.TrySkipIisCustomErrors = true;

                IController errorsController = new ErrorsController();
                var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);
                errorsController.Execute(rc);
            }
        }
Ejemplo n.º 24
0
        protected void Application_Error(object sender, EventArgs e)
        {
#if true
            var httpContext = ((MvcApplication)sender).Context;

            //var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
            //var currentController = " ";
            //var currentAction = " ";

            //if (currentRouteData != null)
            //{
            //    if (currentcontroller != null &&
            //        !String.IsNullOrEmpty(currentcontroller.ToString()))
            //    {
            //        currentController = currentcontroller.ToString();
            //    }

            //    if (currentaction != null &&
            //        !String.IsNullOrEmpty(currentaction.ToString()))
            //    {
            //        currentAction = currentaction.ToString();
            //    }
            //}

            var ex = Server.GetLastError();

            AppLogger.Logger.Fatal(ex);

            var controller    = "Errors";
            var action        = "Index";
            var exceptionPath = string.Empty;

            httpContext.ClearError();
            httpContext.Response.Clear();

            if (ex is HttpException)
            {
                var httpEx = ex as HttpException;

                switch (httpEx.GetHttpCode())
                {
                case 404:
                    controller = "Errors";
                    action     = "NotFound";

                    break;

                // others if any
                case 401:
                    controller = "Errors";
                    action     = "Unauthorized";

                    break;

                default:
                    controller = "Errors";
                    action     = "Oops";
                    break;
                }
            }
            else if (ex is InvalidOperationException)
            {
                controller = "Errors";
                action     = "Oops";
            }
            else if (ex is System.Data.Entity.Core.EntityException)
            {
                controller = "Errors";
                action     = "Oops";
                if (ex.Message.Contains("The underlying provider failed on Open"))
                {
                    try
                    {
                        // Email me the error right away, or send me an sms
                        var alertPhoneNumber = Environment.GetEnvironmentVariable("ALERT_PHONE_NUMBER");
                    }
                    catch (Exception alertEx)
                    {
                        // just log it for now
                        AppLogger.Logger.Error(alertEx);
                    }
                }
            }
            else
            {
                httpContext.Response.StatusCode             = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
                httpContext.Response.TrySkipIisCustomErrors = true;
                controller = "Errors";
                action     = "Index";

                exceptionPath = HttpContext.Current.Request.Url.AbsolutePath;
            }

            HttpContext.Current.Response.RedirectToRoute(controller.ToString(), new { action, details = ex.Message });
#else
            Exception exception = Server.GetLastError();
            Server.ClearError();

            var routeData = new RouteData();
            controller = "Home";
            action     = "Index";
            routeData.Values["exception"] = exception;

            if (exception.GetType() == typeof(HttpException))
            {
                Response.StatusCode = ((HttpException)exception).GetHttpCode();
                routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
            }
            else
            {
                Response.StatusCode = 500;
                routeData.Values.Add("statusCode", 500);
            }

            IController controller = new ErrorsController();
            var         rc         = new RequestContext(new HttpContextWrapper(Context), routeData);
            controller.Execute(rc);
#endif
        }