예제 #1
0
        private void ShowCustomErrorPage(Exception exception)
        {
            HttpException httpException = exception as HttpException;

            if (httpException == null)
            {
                httpException = new HttpException(500, "Internal Server Error", exception);
            }

            Response.Clear();
            RouteData routeData = new RouteData();

            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
            case 403:
                routeData.Values.Add("action", "AccessDenied");
                break;

            case 404:
                routeData.Values.Add("action", "NotFound");
                break;

            case 500:
                routeData.Values.Add("action", "InternalError");
                break;

            case 502:
                routeData.Values.Add("action", "Unavailable");
                break;

            default:
                routeData.Values.Add("action", "Index");
                routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                break;
            }

#if !DEBUG
            ErrorLogManager errorLogManager = new ErrorLogManager();
            ErrorLog        log             = new ErrorLog()
            {
                Message    = exception.Message,
                Name       = exception.GetType().FullName,
                StatusCode = httpException.GetHttpCode(),
                StackTrace = exception.StackTrace
            };
            errorLogManager.AddAsync(log).ContinueWith(x => x.Dispose());
#endif

            Server.ClearError();
            IController controller = new ErrorController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
예제 #2
0
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }

            var statusCode = (int)HttpStatusCode.InternalServerError;

            if (filterContext.Exception is HttpException)
            {
                statusCode = (filterContext.Exception as HttpException).GetHttpCode();
            }
            else if (filterContext.Exception is UnauthorizedAccessException)
            {
                //to prevent login prompt in IIS
                // which will appear when returning 401.
                statusCode = (int)HttpStatusCode.Forbidden;
            }
            else
            {
                statusCode = 500;
            }

#if !DEBUG
            // Log the exception
            ErrorLogManager errorLogManager = new ErrorLogManager();
            ErrorLog        log             = new ErrorLog()
            {
                Message        = filterContext.Exception.Message,
                Name           = filterContext.Exception.GetType().FullName,
                StatusCode     = statusCode,
                StackTrace     = filterContext.Exception.StackTrace,
                ActionName     = (string)filterContext.RouteData.Values["action"],
                ControllerName = (string)filterContext.RouteData.Values["controller"]
            };
            errorLogManager.AddAsync(log).ContinueWith(x => x.Dispose());
#endif

            var result = CreateActionResult(filterContext, statusCode);
            filterContext.Result = result;

            // Prepare the response code.
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode             = statusCode;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }