How To Prevent A Single Block Exception Taking Down Your Whole Episerver Page

Episerver provides content editors with a lot of flexibility for configuring content.  Gone are the days of having lots and lots of different templates.  On my projects, I would typically have less than 20 page types and then x number of blocks.  One common problem that keeps cropping up, is how to deal with block exceptions?

On some of the more content/feature heavy pages, I’ve had page instances that might have 100+ blocks loaded onto it. If one of those blocks error for whatever reason, the whole page falls over and the user is shown a 500 error.  In the majority of situations, the better solution is to have Episerver deal with the error quietly in the background and then render the page out as you expect and in most circumstances have a very small area of a page look a bit funky or not display.

Luckily, Episerver is very customizable and allows you to hook into it pretty easily.  In today’s guide, we’re going to go over some code that will hide all exceptions thrown by blocks for you to deal with yourself.

IContentRenderer

In Episerver the IContentRenderer is the way Episerver renders partial content.  If we want to create a custom rule before we render content, we can implement this interface and then tell structure map to use it, instead of the default implementation.

public class ErrorHandlingContentRenderer : IContentRenderer
{
private readonly MvcContentRenderer _mvcRenderer;
public CustomContentRenderer(MvcContentRenderer mvcRenderer)
{
_mvcRenderer = mvcRenderer;
}
public void Render(HtmlHelper helper, PartialRequest partialRequestHandler, IContentData contentData, TemplateModel templateModel)
{
try
{
mvcRenderer.Render(helper, partialRequestHandler, contentData, templateModel);
}
catch (Exception ex)
{
// Deal With It
}
}

The code is very simple to implement. First, we implement IContentRenderer which has one method:

void Render(HtmlHelper helper, PartialRequest partialRequestHandler, IContentData contentData, TemplateModel templateModel);

Render() is the method that you need to override in order to add the custom logic to handle exceptions. Next, we pass in the Episerver MvcContentRenderer into the constructor, found in Episerver.dll.

Lastly, we manually call the Render method and wrap it in a try/catch and that’s it.  This snippet will now be called whenever we try to run a block.  If the block throws an exception for whatever reason, the try/catch will hide it and the rest of the page will load as expected. The structure map code might look similar to this in your configuration container:

container.For<IContentRenderer>().Use<CustomContentRenderer>();

Conclusion

In today’s guide, we’ve covered some of the issues you might bump into when you’re working with blocks in EPiServer. One frequent point is having a whole page fall over if a single block throws an unexpected exception.  How you deal with it will largely be based on your web strategy.  One common way is to hide the exceptions and log them somewhere.

If you want to adopt this approach, you can implement your own content render.  With a bit of structure map trickery, you can ensure your class gets the call whenever a block is rendered.  All you need to do is implement the Render method however you feel fit.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *