public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            bool      isUserException = false;
            Exception exception       = actionExecutedContext.Exception;
            Type      exceptionType   = exception.GetType();

            HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
            StringBuilder  sb         = new StringBuilder();

            if (exceptionType == typeof(UnauthorizedAccessException))
            {
                statusCode = HttpStatusCode.Unauthorized;
                sb.Append(exception.GetExceptionMessage());
            }
            else if (exceptionType == typeof(ArgumentException))
            {
                statusCode = HttpStatusCode.NotFound;
                sb.Append(exception.GetExceptionMessage());
            }
            else if (exceptionType == typeof(ApiException))
            {
                ApiException failure = (ApiException)exception;
                sb.Append(failure.GetExceptionMessage());
                statusCode      = failure.StatusCode;
                isUserException = true;
            }
            else if (exceptionType == typeof(ApplicationException))
            {
                ApplicationException applicationException = (ApplicationException)exception;
                sb.Append(applicationException.GetExceptionMessage());
                statusCode      = HttpStatusCode.BadRequest;
                isUserException = true;
            }
            else
            {
                sb.Append(exception.GetExceptionMessage());
            }

            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(statusCode, new { ErrorMessage = sb.ToString(), StatusCode = statusCode });

            if (!isUserException)
            {
                Logger.Error(sb.ToString());
            }

            base.OnException(actionExecutedContext);
        }
Example #2
0
        // public override void OnException(ExceptionContext context)
        // {
        //    new { ErrorMessage = sb.ToString(), StatusCode = statusCode }
        //    var jsonResult = new JsonResult(new { ErrorMessage = context.Exception.Message, });
        //    context.Result = jsonResult;
        //    base.OnException(context);
        //}

        public override void OnException(ExceptionContext context)
        {
            bool      isUserException = false;
            Exception exception       = context.Exception;
            Type      exceptionType   = exception.GetType();

            HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
            StringBuilder  sb         = new StringBuilder();

            if (exceptionType == typeof(UnauthorizedAccessException))
            {
                statusCode = HttpStatusCode.Unauthorized;
                sb.Append(exception.GetExceptionMessage());
            }
            else if (exceptionType == typeof(ArgumentException))
            {
                statusCode = HttpStatusCode.NotFound;
                sb.Append(exception.GetExceptionMessage());
            }
            else if (exceptionType == typeof(ApiException))
            {
                ApiException failure = (ApiException)exception;
                sb.Append(failure.GetExceptionMessage());
                statusCode      = failure.StatusCode;
                isUserException = true;
            }

            else
            {
                sb.Append(exception.GetExceptionMessage());
            }

            var jsonResult = new JsonResult(new { ErrorMessage = sb.ToString(), StatusCode = statusCode });

            jsonResult.StatusCode = (int)statusCode;
            context.Result        = jsonResult;


            base.OnException(context);
        }
        public override void OnException(ExceptionContext filterContext)
        {
            bool isUserException = false;

            if (filterContext.HttpContext.Request.ContentType.StartsWith(
                    "application/json", StringComparison.OrdinalIgnoreCase))
            {
                Exception exception = filterContext.Exception;
                if (exception != null)
                {
                    Type exceptionType = exception.GetType();

                    HttpStatusCode statusCode = HttpStatusCode.InternalServerError;
                    StringBuilder  sb         = new StringBuilder();
                    if (exceptionType == typeof(UnauthorizedAccessException))
                    {
                        statusCode = HttpStatusCode.Unauthorized;
                        sb.Append(exception.GetExceptionMessage());
                    }
                    else if (exceptionType == typeof(ArgumentException))
                    {
                        statusCode = HttpStatusCode.NotFound;
                        sb.Append(exception.GetExceptionMessage());
                    }
                    else if (exceptionType == typeof(ApiException))
                    {
                        ApiException failure = (ApiException)exception;
                        sb.Append(failure.GetExceptionMessage());
                        statusCode      = failure.StatusCode;
                        isUserException = true;
                    }
                    else if (exceptionType == typeof(ApplicationException))
                    {
                        ApplicationException applicationException = (ApplicationException)exception;
                        sb.Append(applicationException.GetExceptionMessage());
                        statusCode      = HttpStatusCode.Forbidden;
                        isUserException = true;
                    }
                    else
                    {
                        sb.Append(exception.GetExceptionMessage());
                    }

                    filterContext.HttpContext.Response.StatusCode = (int)statusCode;
                    filterContext.ExceptionHandled = true;
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                    filterContext.Result = new Framework.Web.Mvc.JsonNetResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                        Data = new { ErrorMessage = sb.ToString(), StatusCode = statusCode }
                    };
                }
            }

            if (!isUserException)
            {
                Logger.Error(filterContext.Exception, WebConstants.FilterComponent);
            }

            base.OnException(filterContext);
        }