public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is Mvc5Application1Exception)
            {
                var    exception = (Mvc5Application1Exception)filterContext.Exception;
                string fieldName = exception.FieldName ?? string.Empty;

                filterContext.Controller.ViewData.ModelState.AddModelError(fieldName, exception.Message);
                filterContext.CreateViewResult();
                filterContext.ExceptionHandled = true;

                _logger.LogInformational(filterContext.Exception.Message);
            }

            if (!filterContext.ExceptionHandled && filterContext.Exception is ApplicationException)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result           = new RedirectResult("~/Home/PermissionError");
                    filterContext.ExceptionHandled = true;
                }
                else
                {
                    filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary
                    {
                        { "action", "SignOut" },
                        { "controller", "Auth" },
                        { "area", "" }
                    });

                    filterContext.ExceptionHandled = true;
                }
            }
        }
Example #2
0
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is DbEntityValidationException)
            {
                foreach (var error in ((DbEntityValidationException)filterContext.Exception).EntityValidationErrors.First().ValidationErrors)
                {
                    if (string.IsNullOrEmpty(error.PropertyName))
                    {
                        filterContext.Controller.ViewData.ModelState.AddModelError("", error.ErrorMessage);
                    }
                    else if (filterContext.Controller.ViewData.ModelState.ContainsKey(error.PropertyName) && filterContext.Controller.ViewData.ModelState[error.PropertyName].Errors.Count == 0)
                    {
                        filterContext.Controller.ViewData.ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                    }
                    else
                    {
                        bool found = false;
                        if (filterContext.Controller.ViewData.ModelState.ContainsKey(error.PropertyName))
                        {
                            found = filterContext.Controller.ViewData.ModelState[error.PropertyName].Errors.Any(errorItem => errorItem.ErrorMessage == error.ErrorMessage);
                        }
                        if (!found)
                        {
                            filterContext.Controller.ViewData.ModelState.AddModelError("", error.ErrorMessage);
                        }
                    }
                }

                filterContext.CreateViewResult();

                filterContext.ExceptionHandled = true;

                logger.LogInformational(filterContext.Exception.Message);
            }
        }
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is DbUpdateConcurrencyException)
            {
                filterContext.Controller.ViewData.ModelState.AddModelError(string.Empty, "Unable to save changes. The entity was deleted or updated by another user.");

                filterContext.CreateViewResult();

                filterContext.ExceptionHandled = true;

                logger.LogInformational(filterContext.Exception.Message);
            }
        }
        public virtual void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            Exception exception = filterContext.Exception;

            if (!filterContext.ExceptionHandled && ExceptionType.IsInstanceOfType(exception))
            {
                var message = ErrorMessage ?? exception.Message;
                filterContext.Controller.ViewData.ModelState.AddModelError("", message);

                filterContext.CreateViewResult();

                filterContext.ExceptionHandled = true;
                logger.LogInformational(filterContext.Exception.Message);
            }
        }
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is DbUpdateException)
            {
                var sqlException = filterContext.Exception.GetBaseException();
                if (sqlException is SqlException)
                {
                    var errorMessage = sqlException.Message;
                    var messageCode  = GetMessageCode(errorMessage);
                    if (!string.IsNullOrEmpty(messageCode))
                    {
                        filterContext.Controller.ViewData.ModelState.AddModelError("", Message.ResourceManager.GetString(messageCode, CultureInfo.CurrentUICulture));

                        filterContext.CreateViewResult();

                        filterContext.ExceptionHandled = true;

                        logger.LogInformational(filterContext.Exception.Message);
                    }
                }
            }
        }