Пример #1
0
        protected override void OnException(ExceptionContext filterContext)
        {
            var request = filterContext.HttpContext.Request;

            NTGLogger.LogError(request, filterContext.Exception);

            var contentType = request.Headers["Content-Type"];
            var acceptsJson = request.AcceptTypes.Contains("application/json");

            contentType = contentType != null?contentType.ToLower() : string.Empty;

            if (acceptsJson || contentType.Contains("application/json") || contentType.Contains("multipart/form-data"))
            {
                var errorModel = new BaseAjaxModel();
                errorModel.AddMessage(Message.GLOBAL, new Message(ExceptionMessage, MessageTypes.Error));
                filterContext.HttpContext.Response.StatusCode             = (int)HttpStatusCode.InternalServerError;
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                filterContext.Result = new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = JsonConvert.SerializeObject(errorModel)
                };
                filterContext.ExceptionHandled = true;
            }
            else
            {
                base.OnException(filterContext);
            }
        }
Пример #2
0
        protected void Application_Error(object sender, EventArgs e)
        {
            try
            {
                var ex = Server.GetLastError();
                Server.ClearError();
                NTGLogger.LogError(Context.Request, ex);

                var httpException = ex as HttpException;
                Response.StatusCode = httpException != null?httpException.GetHttpCode() : (int)HttpStatusCode.InternalServerError;

                if (IsMaxRequestExceededException(ex))
                {
                    this.Server.ClearError();
                    Context.ClearError();
                    var errorModel = new BaseAjaxModel();
                    errorModel.AddMessage(Message.GLOBAL, new Message("Unable to upload. Max file size exceeded", MessageTypes.Error));
                    Context.Response.ContentType = "application/json";
                    Context.Response.StatusCode  = 500;
                    Context.Response.Write(JsonConvert.SerializeObject(errorModel));
                }
            }
            catch (Exception) {
                return;
            }
        }