Skip to main content

Null Explosion Prevention with Datasources!

Sometimes bad data can sneak into the smallest of things and wreak havoc on a site bringing up a dreaded error page (hopefully captured and prettified). We’re going to cover two examples of sneaky bad data blowing up a component and how we can address the problem.

The scenario here revolves around an invalid datasource and a component making use of Glass - this bad data can come about from a publishing issue (such as publishing the page which references a datasource that hasn’t been published or wasn’t in the final workflow step to allow publishing), or some other kind of issue (an invalid item ID, a deleted item still being referenced etc).

1. Getting the Item
In the code for our rendering, we want to grab that datasource item to help out our model, so we did the following:

This confirms that the Guid is a valid format - but doesn’t confirm that it’s a valid item in the database before we pass it to GetItem to grab and cast to our Item_Type.
It wouldn't be unreasonable to expect GetItem to throw a null, which is exactly where I ran into trouble. Instead of returning a null, it blows up and you’ll never reach the next line.

To avoid this error remove references to GetItem<T>, confirm that your item exists, then use Cast<T> instead.
For example:


2. View Rendering Issue
In our same scenario we can come across another issue with the use of View Renderings - this view rendering has a datasource passed to it (instead of using the context item).
For example:
The usual expectation is that the model would return null if we had some kind of issue with the datasource. However, in this situation the bad datasource will blow the rendering up before ever trying to check that If Statement.

The problem isn’t with the view rendering, but getting the rendering within the pipeline. Sitecore will check to see if there’s a value (Sitecore.Mvc.Pipelines.Response.GetRenderer)  and send it back. So, if the datasource is set but wrong, instead of returning null it just sends back the bad data. It attempts to use the default model and promptly blows up - expecting a different model that’s never properly sent.

To fix this, we can override GetViewRenderer with a custom pipeline- modified from this thread:
https://github.com/mikeedwards83/Glass.Mapper/issues/163

This will prevent your view renderings from blowing up from a busted datasource as long as you have the regular null checking on the model set up!

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Using the Source Property

For each of the properties in your template you can set a source for it, this isn’t always used but can improve user experience drastically when done throughout a site. The source field comes in to play whenever you are using any of the following fields: Droplink, Droplist, Droptree, File, Grouped Droplink, Grouped Droplist, Image, Multilist, Treelist, Rich text field and a number of others. There are various ways of setting these up to achieve different results – but in general you are using the source to limit the set of items that can be used, and this requirement can also help you determine what kind of field to use. For example, if you have a Set of items all split down into sub folders and want the content editor to make use of the tree, you could use a TreeList or Drop Tree, but if you just want a set of items without the opportunity to see where those items are – multilists or droplinks are the way to go. For Images you’re generally just specifying where to look for and put th...

Web Forms for Marketers: Send Email

To have the save action for your form actually send email, you will need to change one of the settings, otherwise you will receive this error whenever submitting the form: We experience a technical difficulty while processing your request. Your data may not have been correctly saved. Also in your log (/data/logs/newest log file) you will see this error after the form has been submitted: Exception: System.Net.WebException Message: The remote name could not be resolved: 'example.host' Source: System at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralA...

Getting More from the RTE: Snippets and Styling

Developers often push aside the rich text editor because we would rather of use separate components with data sources. However, sometimes we need to rely on the rich text editor to empower the content editor to edit more directly or to reduce complexity on the page. We can use snippets to reduce complexity or give the editor more options by inserting some predefined HTML into the rich text editor. In this example, we will learn how to get more out of the rich text editor by changing which editor we use, adding new snippets, and styling the added content. First we need to switch from the default basic rich text editor to either one of the other existing editors or to a custom editor. The Rich Text Default view - just the basics: We can change the default editor on a field-by-field basis. Update the Source Property for the Rich Text field so that it references the specific rich text editor we want to use in the core database: On your template, enter the item path for the ...