-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Wrap type conversions in try-catch to prevent crashes due to unhandled exceptions #15640
Conversation
You can test this PR using the following package version. |
|
@cla-avalonia agree |
In my opinion, the error should be added to the Binding log instead of ignoring it completely. |
Typing a letter in a number field is an user error and it would spam the log. |
Without exceptions or messages in log it would be difficult to identify any errors in a Converter. The log level can be customized. |
The exception will be shown in Data Validation of the control. This PR refers only to built in Type Converters, i.e. if you bind an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! Quite an oversight there! (I really wish TypeConverter
had TryX
methods!)
What does the pull request do?
This PR prevents crashes due to illegal conversions in
TargetTypeConverter
, i.e. if a numeric type is bound to a TextBox (only when using CompiledBindings). Yes, currently in 11.1 if you type a letter in a textbox with{Binding NumericType}
, it will crash the whole app (with CompiledBindings).What is the current behavior?
The type conversion exceptions in Compiled Bindings are not handled.
What is the updated/expected behavior with this PR?
The type conversion exceptions are silently ignored - just like before the binding refactor.
How was the solution implemented (if it's not obvious)?
The conversion happens in TryConvert methods, which name implies that a failure should not propagate. It also worked this way before:
Avalonia/src/Avalonia.Base/Utilities/TypeUtilities.cs
Lines 279 to 290 in d407764
So I added analogous
try {} catch {}
in the new typed conveter.Checklist
Breaking changes
n/a
Obsoletions / Deprecations
Fixed issues
Fixes #15546