Catch Template Exceptions

Topics: Customizing Orchard
Oct 7, 2014 at 2:09 PM
We use alternates to customize our site and we had a problem with an outdated alternate and a missing property.

Our Alternate used a property called Description, but the Description was renamed on the model. This causes the system to throw an exception, but Orchard does not seems to handle this and a IIS error was shown.

After some research I coded this IExceptionFilter to handle this situations.
public class ExceptionFilter : FilterProvider, IExceptionFilter {

        private readonly IOrchardServices _orchardServices;

        public ILogger Logger { get; set; }

        public ExceptionFilter(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
            Logger = NullLogger.Instance;
        }

        public void OnException(ExceptionContext filterContext) {

            var shape = _orchardServices.New.ErrorPage();
            shape.Message = filterContext.Exception.Message;
            shape.Exception = filterContext.Exception;

            filterContext.Result = new ShapeResult(filterContext.Controller, shape);
            filterContext.RequestContext.HttpContext.Response.StatusCode = 500;

            filterContext.ExceptionHandled = true;

            // prevent IIS 7.0 classic mode from handling the 404/500 itself
            filterContext.RequestContext.HttpContext.Response.TrySkipIisCustomErrors = true;

            try {
                Logger.Error(filterContext.Exception, "There was an unhandled exception.");
            } catch {
                //a logger exception can't be allowed to interrupt this process
            }
     }
Is this approach correct?

Thanks