Skip to content

Commit edc2fcf

Browse files
Copilotjaviercn
andcommitted
Simplify error handling: combine all checks into single error message
Co-authored-by: javiercn <[email protected]>
1 parent aa79478 commit edc2fcf

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

src/Components/Components/src/PersistentState/PersistentValueProviderComponentSubscription.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,24 +229,10 @@ private static PropertyGetter PropertyGetterFactory((Type type, string propertyN
229229
{
230230
var (type, propertyName) = key;
231231
var propertyInfo = GetPropertyInfo(type, propertyName);
232-
if (propertyInfo == null)
233-
{
234-
// Check if the property exists but is not public to provide a better error message
235-
var nonPublicProperty = type.GetProperty(propertyName, ComponentProperties.BindablePropertyFlags);
236-
if (nonPublicProperty != null)
237-
{
238-
throw new InvalidOperationException(
239-
$"The property '{propertyName}' on component type '{type.FullName}' cannot be used with {nameof(PersistentStateAttribute)} because it is not public. Properties with {nameof(PersistentStateAttribute)} must have a public getter.");
240-
}
241-
242-
throw new InvalidOperationException($"Property {propertyName} not found on type {type.FullName}");
243-
}
244-
245-
// Check if the property is public
246-
if (propertyInfo.GetMethod == null || !propertyInfo.GetMethod.IsPublic)
232+
if (propertyInfo == null || propertyInfo.GetMethod == null || !propertyInfo.GetMethod.IsPublic)
247233
{
248234
throw new InvalidOperationException(
249-
$"The property '{propertyName}' on component type '{type.FullName}' cannot be used with {nameof(PersistentStateAttribute)} because it is not public. Properties with {nameof(PersistentStateAttribute)} must have a public getter.");
235+
$"A public property '{propertyName}' on component type '{type.FullName}' with a public getter wasn't found.");
250236
}
251237

252238
return new PropertyGetter(type, propertyInfo);

src/Components/Components/test/PersistentValueProviderComponentSubscriptionTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,9 @@ public void Constructor_ThrowsClearException_ForPrivateProperty()
660660
new PersistentValueProviderComponentSubscription(
661661
state, componentState, cascadingParameterInfo, serviceProvider, logger));
662662

663-
// Should throw a clear error about non-public properties, not "Property not found"
664-
Assert.Contains("not public", exception.Message);
665-
Assert.Contains("PersistentState", exception.Message);
666-
Assert.DoesNotContain("not found", exception.Message);
663+
// Should throw a clear error about needing a public property with public getter
664+
Assert.Contains("A public property", exception.Message);
665+
Assert.Contains("with a public getter wasn't found", exception.Message);
667666
}
668667

669668
[Fact]
@@ -684,9 +683,9 @@ public void Constructor_ThrowsClearException_ForPrivateGetter()
684683
new PersistentValueProviderComponentSubscription(
685684
state, componentState, cascadingParameterInfo, serviceProvider, logger));
686685

687-
// Should throw a clear error about non-public getter
688-
Assert.Contains("not public", exception.Message);
689-
Assert.Contains("PersistentState", exception.Message);
686+
// Should throw a clear error about needing a public property with public getter
687+
Assert.Contains("A public property", exception.Message);
688+
Assert.Contains("with a public getter wasn't found", exception.Message);
690689
}
691690

692691
[Fact]

0 commit comments

Comments
 (0)