Skip to content

Commit 0f1d096

Browse files
committed
#132 ambiguous unit handling - ensure throw appropriate exception + tidyup
1 parent 21f4759 commit 0f1d096

39 files changed

+220
-59
lines changed

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,7 @@ public void Parse_AmbiguousUnitsThrowsException()
186186
Assert.Throws<AmbiguousUnitParseException>( ()=>unitSystem.Parse<VolumeUnit>("tsp"));
187187

188188
// Act 2
189-
try
190-
{
191-
Volume.Parse("1 tsp");
192-
}
193-
catch (UnitsNetException e)
194-
{
195-
Assert.True(e.InnerException is AmbiguousUnitParseException);
196-
}
189+
Assert.Throws<AmbiguousUnitParseException>(() => Volume.Parse("1 tsp"));
197190
}
198191

199192
[TestCase("m^2", Result = AreaUnit.SquareMeter)]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright(c) 2007 Andreas Gullberg Larsen
2+
// https://github.com/anjdreas/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
24+
namespace UnitsNet
25+
{
26+
public class AmbiguousUnitParseException : UnitsNetException
27+
{
28+
public AmbiguousUnitParseException(string message) : base(message)
29+
{
30+
}
31+
32+
public AmbiguousUnitParseException(string message, Exception innerException) : base(message, innerException)
33+
{
34+
}
35+
}
36+
}

UnitsNet/CustomCode/UnitSystem.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232

3333
namespace UnitsNet
3434
{
35-
public class AbbreviationMap : Dictionary<string, List<int>>
36-
{
37-
38-
}
39-
4035
[PublicAPI]
4136
public partial class UnitSystem
4237
{
@@ -312,5 +307,13 @@ private void LoadDefaultAbbreviatons([NotNull] IFormatProvider culture)
312307
}
313308
}
314309
}
310+
311+
/// <summary>
312+
/// Avoids having too many nested generics for code clarity
313+
/// </summary>
314+
class AbbreviationMap : Dictionary<string, List<int>>
315+
{
316+
317+
}
315318
}
316319
}

UnitsNet/GeneratedCode/UnitClasses/Acceleration.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,11 @@ private static List<Acceleration> ParseWithRegex(string regexString, string str,
432432

433433
converted.Add(From(value, unit));
434434
}
435-
catch (Exception ex)
435+
catch(AmbiguousUnitParseException ambiguousException)
436+
{
437+
throw;
438+
}
439+
catch(Exception ex)
436440
{
437441
var newEx = new UnitsNetException("Error parsing string.", ex);
438442
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/AmplitudeRatio.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ private static List<AmplitudeRatio> ParseWithRegex(string regexString, string st
360360

361361
converted.Add(From(value, unit));
362362
}
363-
catch (Exception ex)
363+
catch(AmbiguousUnitParseException ambiguousException)
364+
{
365+
throw;
366+
}
367+
catch(Exception ex)
364368
{
365369
var newEx = new UnitsNetException("Error parsing string.", ex);
366370
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/Angle.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ private static List<Angle> ParseWithRegex(string regexString, string str, IForma
492492

493493
converted.Add(From(value, unit));
494494
}
495-
catch (Exception ex)
495+
catch(AmbiguousUnitParseException ambiguousException)
496+
{
497+
throw;
498+
}
499+
catch(Exception ex)
496500
{
497501
var newEx = new UnitsNetException("Error parsing string.", ex);
498502
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/Area.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ private static List<Area> ParseWithRegex(string regexString, string str, IFormat
492492

493493
converted.Add(From(value, unit));
494494
}
495-
catch (Exception ex)
495+
catch(AmbiguousUnitParseException ambiguousException)
496+
{
497+
throw;
498+
}
499+
catch(Exception ex)
496500
{
497501
var newEx = new UnitsNetException("Error parsing string.", ex);
498502
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/Density.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ private static List<Density> ParseWithRegex(string regexString, string str, IFor
492492

493493
converted.Add(From(value, unit));
494494
}
495-
catch (Exception ex)
495+
catch(AmbiguousUnitParseException ambiguousException)
496+
{
497+
throw;
498+
}
499+
catch(Exception ex)
496500
{
497501
var newEx = new UnitsNetException("Error parsing string.", ex);
498502
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/Duration.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,11 @@ private static List<Duration> ParseWithRegex(string regexString, string str, IFo
492492

493493
converted.Add(From(value, unit));
494494
}
495-
catch (Exception ex)
495+
catch(AmbiguousUnitParseException ambiguousException)
496+
{
497+
throw;
498+
}
499+
catch(Exception ex)
496500
{
497501
var newEx = new UnitsNetException("Error parsing string.", ex);
498502
newEx.Data["input"] = str;

UnitsNet/GeneratedCode/UnitClasses/ElectricCurrent.g.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,11 @@ private static List<ElectricCurrent> ParseWithRegex(string regexString, string s
412412

413413
converted.Add(From(value, unit));
414414
}
415-
catch (Exception ex)
415+
catch(AmbiguousUnitParseException ambiguousException)
416+
{
417+
throw;
418+
}
419+
catch(Exception ex)
416420
{
417421
var newEx = new UnitsNetException("Error parsing string.", ex);
418422
newEx.Data["input"] = str;

0 commit comments

Comments
 (0)