public void ErrorController_Index() {

      var controller            = new ErrorController();
      var result                = controller.Index("ErrorPage") as ViewResult;
      var model                 = result.Model as PageTopicViewModel;

      controller.Dispose();

      Assert.IsNotNull(model);
      Assert.AreEqual<string>("ErrorPage", model.Title);

    }
Ejemplo n.º 2
0
        public void ErrorControllerErrorActionReturnsSuccess()
        {
            // Arrange
            string expectedUrl     = $"/{ApplicationController.AlertPathName}/500";
            var    errorController = new ErrorController(fakeLogger)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext(),
                },
            };

            // Act
            var result = errorController.Error();

            // Assert
            var statusResult = Assert.IsType <RedirectResult>(result);

            statusResult.Url.Should().Be(expectedUrl);
            A.Equals(false, statusResult.Permanent);
            errorController.Dispose();
        }
Ejemplo n.º 3
0
        protected void Application_Error(object sender, EventArgs e)
        {
            var           httpContext = ((MvcApplication)sender).Context;
            var           ex          = this.Server.GetLastError();
            Controller    controller  = new ErrorController();
            var           routeData   = new RouteData();
            var           action      = "ErrorPage";
            HttpException httpEx      = new HttpException();

            httpContext.ClearError();
            httpContext.Response.Clear();
            if (ex.GetType() == typeof(HttpException))
            {
                httpEx = ex as HttpException;

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

                case 401:
                    action = "SqlException";
                    break;
                }
            }


            string logContent = string.Format(@"{5}#############   ERROR: {0}    #################{5}StatusCode = '{1}', Action = '{2}'{5}Error message: {3}{5}Error details: {4} {5}Inner exception message: {6}",
                                              System.DateTime.Now.ToString(),
                                              (ex is HttpException ? httpEx.GetHttpCode() : 500),
                                              action,
                                              ex.Message,
                                              ex.StackTrace,
                                              Environment.NewLine,
                                              ex.InnerException == null ? string.Empty : ex.InnerException.Message);

            log.Debug(logContent);

            //httpContext.Response.StatusCode = ex is HttpException ? httpEx.GetHttpCode() : 500;
            //httpContext.Response.TrySkipIisCustomErrors = true;

            string Message   = ex.Message;
            string ErrorCode = httpContext.Response.StatusCode.ToString();

            routeData.Values["controller"] = "Error";
            routeData.Values["action"]     = action;
            routeData.Values["message"]    = Message;
            routeData.Values["referer"]    = httpContext.Request.Url.LocalPath;

            bool isAjaxCall = string.Equals("XMLHttpRequest", this.Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);

            this.Context.ClearError();

            if (isAjaxCall)
            {
                //this.Context.Response.BufferOutput = true;
                //this.Context.Response.ContentType = "application/json";
                //this.Context.Response.StatusCode = 200;
                //this.Context.Response.Write(
                //    new JavaScriptSerializer().Serialize(
                //        new
                //        {
                //            CommonError = "CommonError",
                //            url =
                //                System.Configuration.ConfigurationManager.AppSettings["MobileSubFolder"] + "/Common/Error/" + action + "?referer=" + routeData.Values["referer"] + "&message="
                //                + routeData.Values["message"]
                //        }));
            }
            else
            {
                ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
            }

            controller.Dispose();
        }
Ejemplo n.º 4
0
 public void Dispose()
 {
     _errorController.Dispose();
 }
Ejemplo n.º 5
0
        protected void Application_Error(object sender, EventArgs e)
        {
            Log.SaveLogToFile(Log.LogLevel.INFO,
                              "Application_Error",
                              "",
                              "",
                              "",
                              "START",
                              "");
            var           httpContext = ((MvcApplication)sender).Context;
            var           ex          = Server.GetLastError();
            Controller    controller  = new ErrorController();
            var           routeData   = new RouteData();
            var           action      = "SqlException";
            HttpException httpEx      = new HttpException();

            httpContext.ClearError();
            httpContext.Response.Clear();
            if (ex.GetType() == typeof(HttpException))
            {
                httpEx = ex as HttpException;

                switch (httpEx.GetHttpCode())
                {
                case 404:
                    action = "SqlException";
                    break;

                case 401:
                    action = "SqlException";
                    break;
                }
            }

            if (ex.Message.Contains(ExceptionKey.GLV_CMN_DBException))
            {
                action = "SqlException";
            }

            if (ex.Message.Contains(ExceptionKey.GLV_CMN_NotAuthenticated))
            {
                action = "NotAuthenticated";
            }

            if (ex.Message.Contains(ExceptionKey.GLV_CMN_InvalidAccessException))
            {
                action = "NotPermission";
            }

            if (ex.Message.Contains(ExceptionKey.GLV_CMN_NotFoundException))
            {
                action = "NotFound";
            }

            if (ex.Message.Contains(ExceptionKey.GLV_CMN_LoginException))
            {
                action = "Login";
            }

            httpContext.Response.StatusCode = ex is HttpException?httpEx.GetHttpCode() : 500;

            httpContext.Response.TrySkipIisCustomErrors = true;

            string Message   = ex.Message;
            string ErrorCode = httpContext.Response.StatusCode.ToString();

            routeData.Values["controller"] = "Error";
            routeData.Values["action"]     = action;
            routeData.Values["message"]    = Message;
            routeData.Values["referer"]    = httpContext.Request.Url.LocalPath;

            bool isAjaxCall = string.Equals("XMLHttpRequest", Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);

            Context.ClearError();
            if (isAjaxCall)
            {
                Context.Response.ContentType = "application/json";
                Context.Response.StatusCode  = 200;
                Context.Response.Write(
                    new JavaScriptSerializer().Serialize(
                        new { CommonError = "CommonError", url = "/Common/Error/" + action + "?referer=" + routeData.Values["referer"] + "&message=" + routeData.Values["message"] }
                        )
                    );
            }
            else
            {
                ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
            }
            controller.Dispose();

            Log.SaveLogToFile(Log.LogLevel.INFO,
                              "Application_Error",
                              "",
                              "",
                              "",
                              "END",
                              "");
        }