diff --git a/csharp/PhoneNumbers.Extensions.Test/TestPhoneNumberTypeConverter.cs b/csharp/PhoneNumbers.Extensions.Test/TestPhoneNumberTypeConverter.cs index aa0da2ae..fbdf4235 100644 --- a/csharp/PhoneNumbers.Extensions.Test/TestPhoneNumberTypeConverter.cs +++ b/csharp/PhoneNumbers.Extensions.Test/TestPhoneNumberTypeConverter.cs @@ -1,3 +1,4 @@ +using System; using Xunit; namespace PhoneNumbers.Extensions.Test @@ -15,6 +16,14 @@ public void ConvertFrom_ParsesString() Assert.Equal(6192987704UL, number.NationalNumber); } + [Fact] + public void ConvertFrom_InvalidString_ThrowsFormatException() + { + var ex = Assert.Throws(() => Converter.ConvertFrom("not-a-number")); + + Assert.IsType(ex.InnerException); + } + [Fact] public void ConvertFrom_MatchesTryParse_ForEqualityAndHashing() { diff --git a/csharp/PhoneNumbers.Extensions/PhoneNumberTypeConverter.cs b/csharp/PhoneNumbers.Extensions/PhoneNumberTypeConverter.cs index 920e909d..4aa571ed 100644 --- a/csharp/PhoneNumbers.Extensions/PhoneNumberTypeConverter.cs +++ b/csharp/PhoneNumbers.Extensions/PhoneNumberTypeConverter.cs @@ -17,9 +17,21 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT => sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - => value is string stringValue - ? Util.Parse(stringValue, null) - : base.ConvertFrom(context, culture, value); + { + if (value is not string stringValue) + { + return base.ConvertFrom(context, culture, value); + } + + try + { + return Util.Parse(stringValue, null); + } + catch (NumberParseException ex) + { + throw new FormatException($"'{stringValue}' is not a valid phone number.", ex); + } + } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => destinationType == typeof(string) || base.CanConvertTo(context, destinationType); diff --git a/csharp/PhoneNumbers.MetadataBuilder/Program.cs b/csharp/PhoneNumbers.MetadataBuilder/Program.cs index a51312c0..de33c088 100644 --- a/csharp/PhoneNumbers.MetadataBuilder/Program.cs +++ b/csharp/PhoneNumbers.MetadataBuilder/Program.cs @@ -189,7 +189,7 @@ private static int BuildLocaleNames(string inputFile, string outputDir) var byCountry = ParseLocaleText(inputFile); foreach (var country in byCountry) { - var outPath = Path.Join(outputDir, country.Key); + var outPath = Path.Join(outputDir, Path.GetFileName(country.Key)); using var gz = new GZipStream(File.Create(outPath), CompressionLevel.SmallestSize); BuildPrefixMapFromBin.WriteLocaleNames(gz, country.Value); }