Skip to content

Commit 995a14a

Browse files
committed
Improve exceptions
1 parent 5242fca commit 995a14a

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

SpreadsheetUtility/Spreadsheet.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,17 @@ void WriteData<T>(List<PropertyInfo> properties, IEnumerable<T> source)
250250
for (int x = 0; x < properties.Count(); x++)
251251
{
252252
var row = y + 1;
253-
var value = (string?)Convert.ChangeType(properties[x].GetValue(source.ElementAt(y)),
254-
typeof(string), CultureInfo.InvariantCulture);
253+
string? value;
254+
255+
try
256+
{
257+
value = (string?)Convert.ChangeType(properties[x].GetValue(source.ElementAt(y)),
258+
typeof(string), CultureInfo.InvariantCulture);
259+
}
260+
catch (Exception innerException)
261+
{
262+
throw new Exception($"Could convert value of property '{properties[x].Name}' to string. The value would have been written to cell '{Cell(row, y)}'", innerException);
263+
}
255264

256265
if (properties[x].PropertyType == typeof(string))
257266
{
@@ -285,8 +294,15 @@ IEnumerable<T> ReadData<T>(Dictionary<PropertyInfo, int> properties)
285294
if (!property.Key.CanWrite)
286295
continue;
287296

288-
property.Key.SetValue(entry, Convert.ChangeType(value,
289-
property.Key.PropertyType, CultureInfo.InvariantCulture));
297+
try
298+
{
299+
property.Key.SetValue(entry, Convert.ChangeType(value,
300+
property.Key.PropertyType, CultureInfo.InvariantCulture));
301+
}
302+
catch (Exception innerException)
303+
{
304+
throw new Exception($"Could not set value '{value}' to property '{property.Key.Name}'. The value was read in cell '{Cell(property.Value, y)}'", innerException);
305+
}
290306
}
291307

292308
data.Add(entry);

SpreadsheetUtility/SpreadsheetUtility.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RepositoryUrl>https://github.com/Planktomas/SpreadsheetUtility</RepositoryUrl>
1212
<PackageTags>excel;spreadsheet;utility;sheet;lightweight;report;xlsx;xls;review;analysis;data</PackageTags>
1313
<Authors>Tomas Kucinskas</Authors>
14-
<Version>1.3.0</Version>
14+
<Version>1.3.1</Version>
1515
<Description>Ultra lightweight spreadsheet utility to display processed collections of data and occasionally reading it.</Description>
1616
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)