Custom Error Handling
This commit is contained in:
@@ -11,7 +11,7 @@ using System.Web.Mvc;
|
||||
namespace Disco.Services.Web
|
||||
{
|
||||
[DiscoAuthorize]
|
||||
public abstract class AuthorizedController : Controller
|
||||
public abstract class AuthorizedController : BaseController
|
||||
{
|
||||
public AuthorizationToken Authorization
|
||||
{
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Services.Web
|
||||
{
|
||||
[HandleError]
|
||||
public class BaseController : Controller
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ using System.Web.Mvc;
|
||||
namespace Disco.Services.Web
|
||||
{
|
||||
[OutputCache(Duration = 0, Location = System.Web.UI.OutputCacheLocation.None)]
|
||||
public abstract class DatabaseController : Controller
|
||||
public abstract class DatabaseController : BaseController
|
||||
{
|
||||
protected DiscoDataContext Database;
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Services.Web
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
|
||||
public class HandleErrorAttribute : FilterAttribute, IExceptionFilter
|
||||
{
|
||||
private readonly object _typeId = new object();
|
||||
private const string ViewArea = null;
|
||||
private const string ViewMaster = "_Layout";
|
||||
private const string ViewPage = "Error";
|
||||
|
||||
public virtual void OnException(ExceptionContext filterContext)
|
||||
{
|
||||
if (filterContext == null)
|
||||
throw new ArgumentNullException("filterContext");
|
||||
|
||||
if (!filterContext.IsChildAction && (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled))
|
||||
{
|
||||
Exception ex = filterContext.Exception;
|
||||
HttpException httpException = new HttpException(null, ex);
|
||||
int httpExceptionCode = httpException.GetHttpCode();
|
||||
|
||||
switch (httpExceptionCode)
|
||||
{
|
||||
case (int)HttpStatusCode.InternalServerError: // 500
|
||||
case (int)HttpStatusCode.Forbidden: // 403
|
||||
case (int)HttpStatusCode.NotFound: // 403
|
||||
|
||||
filterContext.HandleException();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override object TypeId
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._typeId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Users;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Disco.Services.Web
|
||||
{
|
||||
public static class HelperExtensions
|
||||
{
|
||||
public static void HandleException(this ExceptionContext filterContext)
|
||||
{
|
||||
var ex = filterContext.Exception;
|
||||
var contextResponse = filterContext.HttpContext.Response;
|
||||
|
||||
LogException(ex);
|
||||
|
||||
HttpException httpException = new HttpException(null, ex);
|
||||
int httpExceptionCode = httpException.GetHttpCode();
|
||||
|
||||
string controllerName = (string)filterContext.RouteData.Values["controller"];
|
||||
string actionName = (string)filterContext.RouteData.Values["action"];
|
||||
HandleErrorInfo model = new HandleErrorInfo(ex, controllerName, actionName);
|
||||
ViewResult result = new ViewResult
|
||||
{
|
||||
ViewName = "Error",
|
||||
MasterName = "_Layout",
|
||||
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
|
||||
TempData = filterContext.Controller.TempData
|
||||
};
|
||||
|
||||
filterContext.Result = result;
|
||||
filterContext.ExceptionHandled = true;
|
||||
contextResponse.Clear();
|
||||
contextResponse.StatusCode = httpExceptionCode;
|
||||
contextResponse.TrySkipIisCustomErrors = true;
|
||||
}
|
||||
public static void HandleException(this HttpApplication httpApplication)
|
||||
{
|
||||
var ex = httpApplication.Server.GetLastError();
|
||||
|
||||
LogException(ex);
|
||||
}
|
||||
private static void LogException(Exception Exception)
|
||||
{
|
||||
|
||||
// Log Exception:
|
||||
try
|
||||
{
|
||||
if (Exception is AccessDeniedException)
|
||||
{
|
||||
var accessDeniedException = (AccessDeniedException)Exception;
|
||||
var resource = accessDeniedException.Resource;
|
||||
var httpContext = HttpContext.Current;
|
||||
if (httpContext != null && httpContext.Request != null)
|
||||
resource = string.Format("{0} [{1}]", resource, httpContext.Request.RawUrl);
|
||||
|
||||
AuthorizationLog.LogAccessDenied(UserService.CurrentUserId ?? "[Anonymous]", resource, accessDeniedException.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
Disco.Services.Logging.SystemLog.LogException("Global Application Exception Caught", Exception);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore all logging errors
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user