From 8b962cff0c197f1d8585287f435d2ba03bc48e4a Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Tue, 8 Aug 2017 16:07:16 +0100 Subject: [PATCH 1/2] Catches a dictionary key not found exception & gives better error message with context --- src/Umbraco.Core/Models/ContentBase.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index 0fc3bac044..81808473da 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -264,7 +264,17 @@ namespace Umbraco.Core.Models /// Value as an public virtual object GetValue(string propertyTypeAlias) { - return Properties[propertyTypeAlias].Value; + try + { + return Properties[propertyTypeAlias].Value; + } + catch (KeyNotFoundException ex) + { + var message = string.Format("Cannot find the property with alias '{0}' for the node named '{1}' with id '{2} that uses the document type alias '{3}'", + propertyTypeAlias, this.Name, this.Id, this.ContentTypeBase.Alias); + + throw new Exception(message, ex); + } } /// @@ -275,8 +285,18 @@ namespace Umbraco.Core.Models /// Value as a public virtual TPassType GetValue(string propertyTypeAlias) { - var convertAttempt = Properties[propertyTypeAlias].Value.TryConvertTo(); - return convertAttempt.Success ? convertAttempt.Result : default(TPassType); + try + { + var convertAttempt = Properties[propertyTypeAlias].Value.TryConvertTo(); + return convertAttempt.Success ? convertAttempt.Result : default(TPassType); + } + catch (KeyNotFoundException ex) + { + var message = string.Format("Cannot find the property with alias '{0}' for the node named '{1}' with id '{2} that uses the document type alias '{3}'", + propertyTypeAlias, this.Name, this.Id, this.ContentTypeBase.Alias); + + throw new Exception(message, ex); + } } /// From b60c700e054cf8cd4fbac46de56ba5b50a42efc3 Mon Sep 17 00:00:00 2001 From: Warren Buckley Date: Wed, 9 Aug 2017 10:23:55 +0100 Subject: [PATCH 2/2] Updated to return nulls as opposed to the ex being caught with a meaningful message --- src/Umbraco.Core/Models/ContentBase.cs | 27 ++++++-------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index 81808473da..32eec7f284 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -264,17 +264,7 @@ namespace Umbraco.Core.Models /// Value as an public virtual object GetValue(string propertyTypeAlias) { - try - { - return Properties[propertyTypeAlias].Value; - } - catch (KeyNotFoundException ex) - { - var message = string.Format("Cannot find the property with alias '{0}' for the node named '{1}' with id '{2} that uses the document type alias '{3}'", - propertyTypeAlias, this.Name, this.Id, this.ContentTypeBase.Alias); - - throw new Exception(message, ex); - } + return Properties.Contains(propertyTypeAlias) ? Properties[propertyTypeAlias].Value : null; } /// @@ -285,18 +275,13 @@ namespace Umbraco.Core.Models /// Value as a public virtual TPassType GetValue(string propertyTypeAlias) { - try + if (Properties.Contains(propertyTypeAlias) == false) { - var convertAttempt = Properties[propertyTypeAlias].Value.TryConvertTo(); - return convertAttempt.Success ? convertAttempt.Result : default(TPassType); - } - catch (KeyNotFoundException ex) - { - var message = string.Format("Cannot find the property with alias '{0}' for the node named '{1}' with id '{2} that uses the document type alias '{3}'", - propertyTypeAlias, this.Name, this.Id, this.ContentTypeBase.Alias); + return default(TPassType); + } - throw new Exception(message, ex); - } + var convertAttempt = Properties[propertyTypeAlias].Value.TryConvertTo(); + return convertAttempt.Success ? convertAttempt.Result : default(TPassType); } ///