Esempio n. 1
0
        public override void OnException(ExceptionContext context)
        {
            // Ignore cancel action
            if (context.Exception is OperationCanceledException || context.Exception is ObjectDisposedException)
            {
                context.ExceptionHandled = true;

                return;
            }

            var errorModel = ExceptionContextHelper.GetErrorModel(context, _electLog);

            // Response Result

            if (context.HttpContext.Request.Headers[HeaderKey.Accept] == ContentType.Xml ||
                context.HttpContext.Request.Headers[HeaderKey.ContentType] == ContentType.Xml)
            {
                context.Result = new ContentResult
                {
                    ContentType = ContentType.Xml,
                    Content     = XmlHelper.ToXmlString(errorModel),
                    StatusCode  = context.HttpContext.Response.StatusCode
                };
            }
            else
            {
                context.Result = new ContentResult
                {
                    ContentType = ContentType.Json,
                    Content     = errorModel.ToJsonString(),
                    StatusCode  = errorModel.HttpResponseCode
                };
            }

            context.ExceptionHandled = true;

            // Keep base Exception

            base.OnException(context);
        }
        public override void OnException(ExceptionContext context)
        {
            // Ignore cancel action

            if (context.Exception is OperationCanceledException || context.Exception is ObjectDisposedException)
            {
                context.ExceptionHandled = true;
                return;
            }

            var errorModel = ExceptionContextHelper.GetErrorModel(context, _electLog);

            // Ajax Case
            if (context.HttpContext.Request.IsAjaxRequest())
            {
                context.Result = new JsonResult(errorModel, Core.Models.Constants.Formatting.JsonSerializerSettings);

                context.ExceptionHandled = true;

                // Keep base Exception
                base.OnException(context);

                return;
            }

            // MVC Page
            if (context.Exception is UnauthorizedAccessException)
            {
                // TODO Redirect to un-authorization page
                context.Result = new RedirectToActionResult("Index", "Home", false);
            }
            else
            {
#if DEBUG
                context.ExceptionHandled = true;

                // Keep base Exception
                base.OnException(context);

                return;
#else
                context.Result = new RedirectToActionResult("OopsWithParamEndpoint", "Home", new { statusCode =
                                                                                                       (int)errorModel.Code }, false);
#endif
            }

            // Notify
            var tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);

            tempData.AddOrUpdate(Models.Constants.TempDataKey.Notify,
                                 new NotificationModel
            {
                Title   = "Oops, something went wrong!",
                Message = errorModel.Message,
                Status  = NotificationStatus.Error
            });

            context.ExceptionHandled = true;

            // Keep base Exception
            base.OnException(context);
        }