Skip to content
This repository was archived by the owner on Jun 18, 2018. It is now read-only.

Commit 334cf1e

Browse files
Fixes #111 by wrapping nested ConvertDbToString and ConvertDbToEditor calls in a try catch
1 parent 23060fc commit 334cf1e

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/Our.Umbraco.NestedContent/PropertyEditors/NestedContentPropertyEditor.cs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.DataAnnotations;
34
using System.Linq;
45
using System.Text.RegularExpressions;
@@ -160,15 +161,26 @@ public override string ConvertDbToString(Property property, PropertyType propert
160161
}
161162
else
162163
{
163-
// Create a fake property using the property abd stored value
164-
var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString());
164+
try
165+
{
166+
// Create a fake property using the property abd stored value
167+
var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString());
165168

166-
// Lookup the property editor
167-
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
169+
// Lookup the property editor
170+
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
168171

169-
// Get the editor to do it's conversion, and store it back
170-
propValues[propKey] = propEditor.ValueEditor.ConvertDbToString(prop, propType,
171-
ApplicationContext.Current.Services.DataTypeService);
172+
// Get the editor to do it's conversion, and store it back
173+
propValues[propKey] = propEditor.ValueEditor.ConvertDbToString(prop, propType,
174+
ApplicationContext.Current.Services.DataTypeService);
175+
}
176+
catch (InvalidOperationException)
177+
{
178+
// https://github.com/umco/umbraco-nested-content/issues/111
179+
// Catch any invalid cast operations as likely means courier failed due to missing
180+
// or trashed item so couldn't convert a guid back to an int
181+
182+
propValues[propKey] = null;
183+
}
172184
}
173185

174186
}
@@ -225,18 +237,29 @@ public override object ConvertDbToEditor(Property property, PropertyType propert
225237
}
226238
else
227239
{
228-
// Create a fake property using the property abd stored value
229-
var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString());
240+
try
241+
{
242+
// Create a fake property using the property abd stored value
243+
var prop = new Property(propType, propValues[propKey] == null ? null : propValues[propKey].ToString());
230244

231-
// Lookup the property editor
232-
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
245+
// Lookup the property editor
246+
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
233247

234-
// Get the editor to do it's conversion
235-
var newValue = propEditor.ValueEditor.ConvertDbToEditor(prop, propType,
236-
ApplicationContext.Current.Services.DataTypeService);
248+
// Get the editor to do it's conversion
249+
var newValue = propEditor.ValueEditor.ConvertDbToEditor(prop, propType,
250+
ApplicationContext.Current.Services.DataTypeService);
237251

238-
// Store the value back
239-
propValues[propKey] = (newValue == null) ? null : JToken.FromObject(newValue);
252+
// Store the value back
253+
propValues[propKey] = (newValue == null) ? null : JToken.FromObject(newValue);
254+
}
255+
catch (InvalidOperationException)
256+
{
257+
// https://github.com/umco/umbraco-nested-content/issues/111
258+
// Catch any invalid cast operations as likely means courier failed due to missing
259+
// or trashed item so couldn't convert a guid back to an int
260+
261+
propValues[propKey] = null;
262+
}
240263
}
241264

242265
}

0 commit comments

Comments
 (0)