浏览代码

More info in exception

Piotr Czajkowski 2 月之前
父节点
当前提交
821a1619f0
共有 3 个文件被更改,包括 20 次插入10 次删除
  1. 2 2
      ExcelORM/ExcelORM/ExcelORM.csproj
  2. 15 7
      ExcelORM/ExcelORM/ExcelReader.cs
  3. 3 1
      ExcelORM/ExcelORMTests/ReaderTests.cs

+ 2 - 2
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net8.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>2.4.1</Version>
+        <Version>2.5.0</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -14,7 +14,7 @@
 	<Authors>Piotr Czajkowski</Authors>
 	<Description>Simple library to read/write C# objects from/to Excel files. </Description>
 	<RepositoryType>GitHub</RepositoryType>
-	<PackageReleaseNotes>Updated ClosedXML to 0.104.2 due to security issue.</PackageReleaseNotes>
+	<PackageReleaseNotes>Added Location to exception for easier troubleshooting.</PackageReleaseNotes>
     </PropertyGroup>
 
     <ItemGroup>

+ 15 - 7
ExcelORM/ExcelORM/ExcelReader.cs

@@ -38,16 +38,24 @@ public class ExcelReader : IDisposable
                 var property = type.GetProperty(item.PropertyName);
                 if (property == null) continue;
 
-                if (property.PropertyType.BaseType == typeof(SpecialBase) &&
-                    RuntimeHelpers.GetUninitializedObject(property.PropertyType) is SpecialBase special)
+                try
                 {
-                    special.GetValueFromCell(cell);
+                    if (property.PropertyType.BaseType == typeof(SpecialBase) &&
+                        RuntimeHelpers.GetUninitializedObject(property.PropertyType) is SpecialBase special)
+                    {
+                        special.GetValueFromCell(cell);
 
-                    property.SetValue(current, special);
-                    continue;
-                }
+                        property.SetValue(current, special);
+                        continue;
+                    }
 
-                current.SetPropertyValue(property, cell.Value);
+                    current.SetPropertyValue(property, cell.Value);
+                }
+                catch (ArgumentException e)
+                {
+                    e.Data.Add("Location", $"{cell.Address.ColumnLetter}{cell.Address.RowNumber}");;
+                    throw;
+                }
             }
 
             yield return current;

+ 3 - 1
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -115,6 +115,8 @@ public class ReaderTests
     public void BadDateThrows()
     {
         using var reader = new ExcelReader(BadDate);
-        Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
+        var exception = Assert.Throws<ArgumentException>(() => reader.Read<TestTypes>().ToArray());
+        Assert.True(exception.Data.Contains("Location"));
+        Assert.NotEmpty(exception.Data["Location"]?.ToString() ?? string.Empty);;
     }
 }