Skip to content

Commit

Permalink
Merge pull request #2515 from cwensley/curtis/propertybinding-exceptions
Browse files Browse the repository at this point in the history
Re-throw property binding exceptions
  • Loading branch information
cwensley authored Jul 5, 2023
2 parents b17f1a0 + 8da9111 commit 5c436ca
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
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) { }
}

0 comments on commit 5c436ca

Please sign in to comment.