Sitecore rendering exception handling

What if your rendering throws an exception? Default Sitecore installation will just use ASP.NET behavior and redirect you to the error page (yellowscreen or custom application error page whatever you have configured)

Do you like that?

Sitecore page is a set of separate renderings and why would I see the error page if one of my renderings fails? And what if I am an editor and trying to place the rendering on my page but it just fails to be added? How am I supposed to know how I can fix the problem and add that rendering without contacting developers?

There is probably more ways to do that, but you can implement your own processor for the sitecore/pipelines/mvc.renderRendering pipeline like this:

config

Obviously that will work on Sitecore 7+ and MVC renderings, but I’m sure there is a pipeline or some other way to do that for the WebForms as well

The main idea is that in this processor we wrap the default functionality into try-catch and then render the informative error message which explains why the rendering could not be shown. This way if one rendering fails, the page still works.

I have been using something similar on number of projects and now I have combined that together and created a separate nuget package so it can be reused.

How to install the package:

Install-Package Helpfulcore.RenderingExceptions

How it works:

For example, my rendering (or the controller) has thrown the exception. Now I will see something like this:

unhandled-error-message

You can click “Expand” icon to see the exception stack trace.

unhandled-error-message-expanded

You are going to see this message only if you are in editing mode or the property <customErrors> mode is set to ‘Off’ in your web.config file.

There are two dedicated exception types that could be thrown to display one or another error message:

  • RenderingException – general error. Explain in the exception message the reason why your rendering could not be shown and describe possible ways of solving it.
  • RenderingParametersException – this means that your rendering parameters are missing some fields. For example a lot of renderings require the DataSource to be set.

Now, as a developer, I can handle a number of situations where I know that my rendering could fail. For example there is a property on the website root item which has not been set but my rendering should use it. I can do a simple ‘if’ check and throw

throw1

In this case we will see:

rendering-error-message

If I have a required field in the rendering parameters item for my rendering I can add a simple ‘if’ and throw RenderingParametersException

throw2

It will output next:

param-error-message

As you can see there is a default Sitecore ribbon still appears above the error message and that means that this is still being treated as a rendering by sitecore and you still can do everything with your rendering in the editing mode.

I see it as a way of resolving problems with editors teaching and project support in advance on the development stage. The client won’t be contacting you too frequent as he already has all the explanations on why one or another feature is not working on his website.

Package contents:

  • /bin/Helpfulcore.RenderingExceptions.dll
  • /App_Config/Include/Helpfulcore/Helpfulcore.RenderingExceptions.config
  • /Views/Shared/RenderingExceptions/RenderingException.cshtml
  • /Views/Shared/RenderingExceptions/RenderingParametersException.cshtml\
  • /Views/Shared/RenderingExceptions/UnhandledException.cshtml

Functionality:

  • Error messages are shown in editing mode or if the property <customErrors> mode is set to ‘Off’ in your web.config file. Otherwise the whole rendering is just not shown on the page.
  • The error message still represents your rendering as it was injected through the Sitecore ExecuteRenderer pipeline and that means that you still have all the Sitecore support in editing mode.
  •  The error message is still changeable as the package installs .cshtml files to your project which you can extend or modify.
  • You can implement your own exception type and inherit it from Helpfulcore.RenderingExceptions.RenderingException and then create the dedicated .cshtml file for your exception type. The rendering name should match the exception name.

Source code:

The source code is on github here: https://github.com/vhil/Helpfulcore

Rooms for improvements:

I don’t think I am very good at styling so don’t hesitate to fork the repo and suggest any styling fixes for it.

And you are always welcome to propose your own dedicated exception types and renderings for them.

Check it out folks 🙂

Advertisements

One thought on “Sitecore rendering exception handling

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s