Skip to content

Conversation

@lipchev
Copy link
Collaborator

@lipchev lipchev commented Jan 4, 2025

Fixes #1423 for release-v6
Carry over from #1484

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
  • Avoid some of the unnecessary array allocations

…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
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);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice the new message format, the other one seemed excessively long..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

@angularsen angularsen merged commit 572f151 into angularsen:release/v6 Jan 4, 2025
1 check passed
@lipchev lipchev deleted the unit-parser-exception branch April 11, 2025 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants