🚸UnitParser.Parse should throw AmbiguousUnitParseException before any UnitNotFoundException (#1484)#1494
Merged
Conversation
…en#1484) Fixes angularsen#1423 `UnitParser.Parse<LengthUnit>("MM")` fails due to matching both `Megameter` and `Millimeter` in case-insensitive matching, but matches neither of them in the case-sensitive fallback. It was confusing to get `UnitsNotFoundException` in this case, since case-insensitive usually works for most units. ### Changes - Handle this case and throw `AmbiguousUnitParseException` instead of `UnitNotFoundException` - Describe the case-insensitive units that matched - Fix existing test `Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundException` - Skip retrying with fallback culture if no specific `formatProvider` was given
lipchev
commented
Jan 4, 2025
Comment on lines
+147
to
+152
| [Fact] | ||
| public void Parse_LengthUnit_MM_ThrowsExceptionDescribingTheAmbiguity() | ||
| { | ||
| var ex = Assert.Throws<AmbiguousUnitParseException>(() => UnitsNetSetup.Default.UnitParser.Parse<LengthUnit>("MM")); | ||
| Assert.Equal("""Cannot parse "MM" since it matches multiple units: Megameter ("Mm"), Millimeter ("mm").""", ex.Message); | ||
| } |
Collaborator
Author
There was a problem hiding this comment.
Notice the new message format, the other one seemed excessively long..
lipchev
commented
Jan 4, 2025
Collaborator
Author
There was a problem hiding this comment.
What I said in #1484 about the recursive call requiring additional handling was wrong: I was under the persuasion that if there are multiple matches with the first culture, then we still want to try to disambiguate with the fallback culture, but that makes no sense..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1423 for
release-v6Carry over from #1484
UnitParser.Parse<LengthUnit>("MM")fails due to matching bothMegameterandMillimeterin case-insensitive matching, but matches neither of them in the case-sensitive fallback. It was confusing to getUnitsNotFoundExceptionin this case, since case-insensitive usually works for most units.Changes
AmbiguousUnitParseExceptioninstead ofUnitNotFoundExceptionParse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsUnitNotFoundExceptionformatProviderwas given