From d873382a8a772cbfd795ffd280de4aacd171b9e7 Mon Sep 17 00:00:00 2001 From: Richard Murillo Date: Wed, 26 Jul 2017 10:44:39 -0700 Subject: [PATCH] Catch `DeniedOrNotExistException` when accessing field during map While mapping a `Microsoft.Qwiq.DeniedOrNotExistException` may be encountered while attempting to read a value from `IWorkItem` of a field that does not exist. This change captures the exception and throws an `AttributeMapException` indicating the work item ID, WIT field, and destination property that triggered the error. Resolves #150 --- .../Attributes/AttributeMapperStrategy.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Qwiq.Mapper/Attributes/AttributeMapperStrategy.cs b/src/Qwiq.Mapper/Attributes/AttributeMapperStrategy.cs index 3865be94..0d81cfe8 100644 --- a/src/Qwiq.Mapper/Attributes/AttributeMapperStrategy.cs +++ b/src/Qwiq.Mapper/Attributes/AttributeMapperStrategy.cs @@ -133,7 +133,18 @@ protected internal virtual void MapImpl(Type targetWorkItemType, IWorkItem sourc var fieldName = a.FieldName; var convert = a.RequireConversion; var nullSub = a.NullSubstitute; - var fieldValue = sourceWorkItem[fieldName]; + object fieldValue; + try + { + fieldValue = sourceWorkItem[fieldName]; + } + catch (DeniedOrNotExistException e) + { + var tm = new TypePair(sourceWorkItem, targetWorkItemType); + var pm = new PropertyMap(property, fieldName); + var message = $"Unable to get field value on {sourceWorkItem.Id}."; + throw new AttributeMapException(message, e, tm, pm); + } AssignFieldValue(targetWorkItemType, sourceWorkItem, targetWorkItem, property, fieldName, convert, nullSub, fieldValue); }