-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Catch exception in TryConvertTo #33594
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
Conversation
Add test cases for trying to convert valid and invalid GUID values.
Catch when the converter throws an exception and return false and set the out value to default.
|
Thanks for contributing this, @jeffl8n! Rather than adding a (1) |
|
Thanks for the review @SteveSandersonMS. Should it be a new type specific handler for GUID, or should we wrap the generic type converter with the try/catch?
This would add the try/catch for any of the type converter not already explicitly handled in BindConverter.cs. |
|
I was expecting it to be a GUID-specific parser, i.e., a new case in this long list here: aspnetcore/src/Components/Components/src/BindConverter.cs Lines 1427 to 1508 in a6e00f6
|
Remove try/catch added in TryConvertTo and replace with Guid Converter
| { | ||
| value = default; | ||
| return false; | ||
| } |
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.
I must be missing something here. How come you don't need a try/catch any more? I thought that adding the try/catch was the main reason for the change.
Does structuring the code this way somehow mean there isn't an unhandled exception any more? If you could clarify that would be great!
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.
Yes, because before the GuidConverter would call the ctor which throws on invalid values. #33587 (comment)
You can see it in the stack trace from here #33587 (comment)
Should there be another case for Guid??
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.
That's correct. Calling Guid.TryParse sets the ThrowStyle to None - https://github.com/dotnet/runtime/blob/5221db92ed579f8ffa56f1d8e84f07ba6d896077/src/libraries/System.Private.CoreLib/src/System/Guid.cs#L242
But calling the Guid ctor sets the ThrowStyle to All - https://github.com/dotnet/runtime/blob/5221db92ed579f8ffa56f1d8e84f07ba6d896077/src/libraries/System.Private.CoreLib/src/System/Guid.cs#L210
Should there be another case for Guid??
Do you mean like the check for null/empty string? The Guid class performs several checks for length and formatting, so that current check could be removed as well, but I left it since it would prevent having to call TryParse for the simplest invalid case.
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.
I meant for the typeof(Guid?) type, all the others do both checks like typeof(DateTimeOffset) and typeof(DateTimeOffset?):
aspnetcore/src/Components/Components/src/BindConverter.cs
Lines 1495 to 1502 in a6e00f6
| else if (typeof(T) == typeof(DateTimeOffset)) | |
| { | |
| parser = ConvertToDateTime; | |
| } | |
| else if (typeof(T) == typeof(DateTimeOffset?)) | |
| { | |
| parser = ConvertToNullableDateTime; | |
| } |
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.
You're right. I've just added another case for Nullable Guid.
|
A quick note on this: on investigation I realise this doesn't impact using This change only affects someone calling Since we're late in 6.0 now we're trying not to take inessential changes, so I'll mark this for 7.0. We can merge it into |
* main: Handle more cases with the new entry point pattern (dotnet#33500) [main] Update dependencies from dotnet/runtime dotnet/efcore (dotnet#33560) Refactor LongPolling in Java to avoid stackoverflow (dotnet#33564) Optimize QueryCollection (dotnet#32829) Switch to in-org action (dotnet#33610) Improve Codespaces + C# extension interaction (dotnet#33614)
|
Thanks @SteveSandersonMS. I added the missing tests for the |
|
@jeffl8n Now that 7.0 work is underway, we're ready to proceed with this. However the underlying code has changed too much to merge this directly, since we started using file-scoped namespaces and the default indentation level has changed. I also wanted to tweak the test cases a bit. So, I've filed a continuation at #39649 and will close this one in favour of that. The new PR still credits you as the commit author :) |
Catch when the converter throws an exception and return false and set the out value to default.
Addresses #33587