Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-throw property binding exceptions #2515

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/Eto/Forms/Binding/PropertyBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,20 @@ protected override T InternalGetValue(object dataItem)
if (EnsureProperty(dataItem) && CanRead)
{
var propertyType = typeof(T);
object val = descriptor != null ? descriptor.GetValue(dataItem) : propInfo.GetValue(dataItem);
object val;
try
{
if (descriptor != null)
val = descriptor.GetValue(dataItem);
else if (propInfo != null)
val = propInfo.GetValue(dataItem);
else
return default(T);
}
catch (Exception ex)
{
throw new PropertyBindingException($"Could not get property '{Property}' on '{dataItem?.GetType()}'", ex);
}
if (val != null && !propertyType.IsInstanceOfType(val))
{
try
Expand Down Expand Up @@ -171,10 +184,17 @@ protected override void InternalSetValue(object dataItem, T value)
val = propertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(propertyType) : null;
}
}
if (descriptor != null)
descriptor.SetValue(dataItem, val);
else if (propInfo != null)
propInfo.SetValue(dataItem, val);
try
{
if (descriptor != null)
descriptor.SetValue(dataItem, val);
else if (propInfo != null)
propInfo.SetValue(dataItem, val);
}
catch (Exception ex)
{
throw new PropertyBindingException($"Could not set property '{Property}' on '{dataItem?.GetType()}'", ex);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/Eto/Forms/Binding/PropertyBindingException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Eto.Forms;

/// <summary>
/// Exception when getting/setting values in a <see cref="PropertyBinding{T}" />
/// </summary>
/// <remarks>
/// This exception is thrown explicitly if there is a problem getting or setting the value on the data item.
/// Since using descriptors can sometimes bury the actual stack trace, this can be useful to figure out what property
/// setter/getter is throwing.
/// </remarks>
[System.Serializable]
public class PropertyBindingException : System.Exception
{
/// <summary>
/// Initializes a new instance of the PropertyBindingException class.
/// </summary>
public PropertyBindingException() { }
/// <summary>
/// Initializes a new instance of the PropertyBindingException class with the specified message.
/// </summary>
/// <param name="message">Message of the exception</param>
public PropertyBindingException(string message) : base(message) { }
/// <summary>
/// Initializes a new instance of the PropertyBindingException class with the specified message and inner exception.
/// </summary>
/// <param name="message">Message of the exception</param>
/// <param name="inner">Original exception</param>
public PropertyBindingException(string message, System.Exception inner) : base(message, inner) { }
/// <summary>
/// Initializes a new instance of the PropertyBindingException class from serialization.
/// </summary>
/// <param name="info">Serialization info</param>
/// <param name="context">Streaming context</param>
protected PropertyBindingException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}