44using Microsoft . Xrm . Sdk . Metadata ;
55using Microsoft . Xrm . Sdk . Messages ;
66using System . Collections . Concurrent ;
7+ using Microsoft . PowerPlatform . Dataverse . Client . Utils ;
78
89namespace Microsoft . PowerPlatform . Dataverse . Client
910{
@@ -22,7 +23,7 @@ internal class MetadataUtility
2223 /// </summary>
2324 private ConcurrentDictionary < String , AttributeMetadata > _attributeMetadataCache = new ConcurrentDictionary < String , AttributeMetadata > ( ) ;
2425 /// <summary>
25- /// Global option metadata cache object.
26+ /// Global option metadata cache object.
2627 /// </summary>
2728 private ConcurrentDictionary < String , OptionSetMetadata > _globalOptionMetadataCache = new ConcurrentDictionary < String , OptionSetMetadata > ( ) ;
2829 /// <summary>
@@ -34,7 +35,7 @@ internal class MetadataUtility
3435 /// </summary>
3536 private static Object _lockObject = new Object ( ) ;
3637 /// <summary>
37- /// Last time Entity data was validated.
38+ /// Last time Entity data was validated.
3839 /// </summary>
3940 private DateTime _metadataLastValidatedAt ;
4041
@@ -54,7 +55,7 @@ public MetadataUtility(ServiceClient svcActions)
5455 public void ClearCachedEntityMetadata ( string entityName )
5556 {
5657 TouchMetadataDate ( ) ;
57- // Not clearing the ETC ID's as they do not change...
58+ // Not clearing the ETC ID's as they do not change...
5859 if ( _entityMetadataCache . ContainsKey ( entityName ) )
5960 {
6061 EntityMetadata removedEntData ;
@@ -88,32 +89,39 @@ public List<EntityMetadata> GetAllEntityMetadata(bool onlyPublished, EntityFilte
8889 foreach ( var entity in response . EntityMetadata )
8990 {
9091 if ( _entityMetadataCache . ContainsKey ( entity . LogicalName ) )
91- _entityMetadataCache [ entity . LogicalName ] = entity ; // Update local copy of the entity...
92+ _entityMetadataCache [ entity . LogicalName ] = entity ; // Update local copy of the entity...
9293 else
9394 _entityMetadataCache . TryAdd ( entity . LogicalName , entity ) ;
9495
9596 results . Add ( entity ) ;
96- // Preload the entity data catch as this has been called already
97+ // Preload the entity data catch as this has been called already
9798 if ( _entityNameCache . ContainsKey ( entity . ObjectTypeCode . Value ) )
9899 continue ;
99100 else
100101 _entityNameCache . TryAdd ( entity . ObjectTypeCode . Value , entity . LogicalName ) ;
101102 }
102103 TouchMetadataDate ( ) ;
103104 }
105+ else
106+ {
107+ if ( svcAct . LastException != null )
108+ throw new DataverseOperationException ( $ "Failed to get metadata from Dataverse.", svcAct . LastException ) ;
109+ else
110+ throw new DataverseOperationException ( $ "Failed to get metadata from Dataverse", null ) ;
111+ }
104112
105113 return results ;
106114 }
107115
108116 /// <summary>
109- /// Returns Entity Metadata for requested entity.
117+ /// Returns Entity Metadata for requested entity.
110118 /// Applies returns all data available based on CRM version type
111119 /// </summary>
112120 /// <param name="entityName">Name of the Entity, data is being requested on</param>
113121 /// <returns>Entity data</returns>
114122 public EntityMetadata GetEntityMetadata ( string entityName )
115123 {
116- // Filter the EntityFitlers based on the version of CRM being connected too.
124+ // Filter the EntityFitlers based on the version of CRM being connected too.
117125 if ( svcAct . ConnectedOrgVersion < Version . Parse ( "7.1.0.0" ) )
118126 return GetEntityMetadata ( EntityFilters . Attributes | EntityFilters . Entity | EntityFilters . Privileges | EntityFilters . Relationships , entityName ) ;
119127 else
@@ -180,6 +188,13 @@ public EntityMetadata GetEntityMetadata(EntityFilters requestType, String entity
180188 if ( ! bSelectiveUpdate )
181189 TouchMetadataDate ( ) ;
182190 }
191+ else
192+ {
193+ if ( svcAct . LastException != null )
194+ throw new DataverseOperationException ( $ "Failed to resolve entity metadata for { entityName } .", svcAct . LastException ) ;
195+ else
196+ throw new DataverseOperationException ( $ "Failed to resolve entity metadata for { entityName } .", null ) ;
197+ }
183198 }
184199 return entityMetadata ;
185200 }
@@ -208,22 +223,29 @@ public string GetEntityLogicalName(int entityTypeCode)
208223 {
209224 _entityNameCache . TryAdd ( metadata . ObjectTypeCode . Value , metadata . LogicalName ) ;
210225
211- // reload metadata cache.
226+ // reload metadata cache.
212227 if ( _entityMetadataCache . ContainsKey ( metadata . LogicalName ) )
213228 continue ;
214229 else
215230 _entityMetadataCache . TryAdd ( metadata . LogicalName , metadata ) ;
216231 }
217232 TouchMetadataDate ( ) ;
218233 }
234+ else
235+ {
236+ if ( svcAct . LastException != null )
237+ throw new DataverseOperationException ( $ "Failed to resolve entity name from typecode { entityTypeCode } .", svcAct . LastException ) ;
238+ else
239+ throw new DataverseOperationException ( $ "Failed to resolve entity name from typecode { entityTypeCode } .", null ) ;
240+ }
219241 }
220242 }
221243 _entityNameCache . TryGetValue ( entityTypeCode , out name ) ;
222244 return name ;
223245 }
224246
225247 /// <summary>
226- ///
248+ ///
227249 /// </summary>
228250 /// <param name="entityName"></param>
229251 /// <param name="attributeName"></param>
@@ -249,13 +271,20 @@ public AttributeMetadata GetAttributeMetadata(string entityName, string attribut
249271 _attributeMetadataCache . TryAdd ( String . Format ( CultureInfo . InvariantCulture , "{0}.{1}" , entityName , attributeName ) , attributeMetadata ) ;
250272 _metadataLastValidatedAt = DateTime . UtcNow ;
251273 }
274+ else
275+ {
276+ if ( svcAct . LastException != null )
277+ throw new DataverseOperationException ( $ "Failed to resolve attribute metadata for { attributeName } in entity { entityName } .", svcAct . LastException ) ;
278+ else
279+ throw new DataverseOperationException ( $ "Failed to resolve attribute metadata for { attributeName } in entity { entityName } .", null ) ;
280+ }
252281 }
253282 }
254283 return attributeMetadata ;
255284 }
256285
257286 /// <summary>
258- ///
287+ ///
259288 /// </summary>
260289 /// <param name="entityName"></param>
261290 /// <returns></returns>
@@ -264,7 +293,7 @@ public List<AttributeMetadata> GetAllAttributesMetadataByEntity(string entityNam
264293 EntityMetadata entityMetadata = GetEntityMetadata ( entityName ) ;
265294 if ( entityMetadata != null )
266295 {
267- // Added to deal with failed call to CRM.
296+ // Added to deal with failed call to CRM.
268297 if ( entityMetadata . Attributes != null )
269298 {
270299 List < AttributeMetadata > results = new List < AttributeMetadata > ( ) ;
@@ -281,7 +310,7 @@ public List<AttributeMetadata> GetAllAttributesMetadataByEntity(string entityNam
281310 }
282311
283312 /// <summary>
284- ///
313+ ///
285314 /// </summary>
286315 /// <param name="entityName"></param>
287316 /// <returns></returns>
@@ -304,7 +333,7 @@ public List<String> GetRequiredAttributesByEntity(string entityName)
304333 }
305334
306335 /// <summary>
307- /// Retrieve Global OptionSet Information.
336+ /// Retrieve Global OptionSet Information.
308337 /// </summary>
309338 /// <param name="optionSetName"></param>
310339 /// <returns></returns>
@@ -313,7 +342,7 @@ public OptionSetMetadata GetGlobalOptionSetMetadata(string optionSetName)
313342 if ( string . IsNullOrEmpty ( optionSetName ) )
314343 return null ;
315344
316- ValidateMetadata ( ) ; // Check to see if Metadata has expired.
345+ ValidateMetadata ( ) ; // Check to see if Metadata has expired.
317346
318347 if ( _globalOptionMetadataCache . ContainsKey ( optionSetName ) )
319348 return _globalOptionMetadataCache [ optionSetName ] ;
@@ -332,11 +361,18 @@ public OptionSetMetadata GetGlobalOptionSetMetadata(string optionSetName)
332361 return _globalOptionMetadataCache [ optionSetName ] ;
333362 }
334363 }
364+ else
365+ {
366+ if ( svcAct . LastException != null )
367+ throw new DataverseOperationException ( $ "Failed to resolve global optionset metadata for { optionSetName } .", svcAct . LastException ) ;
368+ else
369+ throw new DataverseOperationException ( $ "Failed to resolve global optionset metadata for { optionSetName } .", null ) ;
370+ }
335371 return null ;
336372 }
337373
338374 /// <summary>
339- ///
375+ ///
340376 /// </summary>
341377 private void ValidateMetadata ( )
342378 {
0 commit comments