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); }
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); }