/// <summary>
        ///     Called when an exception occurs.
        /// </summary>
        /// <param name="filterContext">The action-filter context.</param>
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
                throw new ArgumentNullException(nameof(filterContext));

            if (filterContext.IsChildAction)
                return;

            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
                return;

            var exception = filterContext.Exception;
            if (!ExceptionType.IsInstanceOfType(exception))
                return;

            var args = new HandleErrorEventArgs(filterContext);

            try
            {
                OnLogException(args);
            }
            finally
            {
                if (args.ReturnValue != null)
                {
                    if (filterContext.Controller.TempData.ContainsKey(_returnValueKey))
                        filterContext.Controller.TempData[_returnValueKey] = args.ReturnValue;
                    else
                        filterContext.Controller.TempData.Add(_returnValueKey, args.ReturnValue);
                }

                var item = (string)filterContext.RouteData.Values["controller"];
                var str = (string)filterContext.RouteData.Values["action"];
                var handleErrorInfo = new HandleErrorInfo(filterContext.Exception, item, str);

                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    var message = filterContext.Exception is UnauthorizedHttpException
                                      ? filterContext.Exception.Message
                                      : INTERNAL_SERVER_ERROR;

                    if (filterContext.Exception is UnauthorizedHttpException)
                    {
                        filterContext.Result = new JsonResult
                                               {
                                                   JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                                                   ContentType = "application/json",
                                                   ContentEncoding = Encoding.UTF8,
                                                   Data = new JsonData { Data = new { }, Errors = new { Server = new { errors = new[] { message } } } }
                                               };
                    }
                }
                else
                {
                    filterContext.Result = new ViewResult
                                           {
                                               ViewName = View,
                                               MasterName = Master,
                                               ViewData = new ViewDataDictionary<HandleErrorInfo>(handleErrorInfo),
                                               TempData = filterContext.Controller.TempData
                                           };
                }

                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.StatusCode = new HttpException(null, exception).GetHttpCode();
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }
 private void OnLogException(HandleErrorEventArgs handleErrorEventArgs) => LogException?.Invoke(this, handleErrorEventArgs);