英文:
How handle Multiple Type convertor exception in CSVhelper
问题
最近我将CSVHelper库从2.7.1版本升级到30版本。我已解决所有构建错误。但是在升级后,异常未被记录。
旧代码:
csvReader.Configuration.ReadingExceptionCallback =
(ex, row) =>
{
if (ex is CsvHelper.TypeConversion.CsvTypeConverterException)
{
foreach (DictionaryEntry error in ex.Data)
{
AddRowError(row.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(row) + "'");
}
}
else if (ex is FormatException)
{
AddRowError(row.Row, ex.Message + " Column Name: '" + GetColumnName(row) + "' Column Value: '" + GetColumnValue(row) + "'");
}
else
{
AddRowError(row.Row, string.Format("Line[{0}]: {1}", row.Row, ex.StackTrace));
}
};
新代码:
ReadingExceptionOccurred = args =>
{
var context = args.Exception.Context;
if (args.Exception is CsvHelper.TypeConversion.TypeConverterException)
{
foreach (DictionaryEntry error in args.Exception.Data)
{
AddRowError(context.Parser.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(context.Reader) + "'");
}
}
else if (args.Exception.GetType() == typeof(FormatException))
{
AddRowError(context.Parser.Row, args.Exception.Message + " Column Name: '" + GetColumnName(context.Reader) + "' Column Value: '" + GetColumnValue(context.Reader) + "'");
}
else
{
AddRowError(context.Parser.Row, string.Format("Line[{0}]: {1}", context.Parser.Row, args.Exception.StackTrace));
}
return false;
};
英文:
Recently I upgraded CSVHelper libraries from 2.7.1 to 30 version.
I have resolved all build errors. But after upgradation exceptions are not logging.
Old Code:
csvReader.Configuration.ReadingExceptionCallback =
(ex, row) =>
{
if (ex is CsvHelper.TypeConversion.CsvTypeConverterException)
{
foreach (DictionaryEntry error in ex.Data)
{
AddRowError(row.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(row) + "'");
}
}
else if (ex is FormatException)
{
AddRowError(row.Row, ex.Message + " Column Name: '" + GetColumnName(row) + "' Column Value: '" + GetColumnValue(row) + "'");
}
else
{
AddRowError(row.Row, string.Format("Line[{0}]: {1}", row.Row, ex.StackTrace));
}
};
New Code:
ReadingExceptionOccurred = args =>
{
var context = args.Exception.Context;
if (args.Exception is CsvHelper.TypeConversion.TypeConverterException)
{
foreach (DictionaryEntry error in args.Exception.Data)
{
AddRowError(context.Parser.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(context.Reader) + "'");
}
}
else if (args.Exception.GetType() == typeof(FormatException))
{
AddRowError(context.Parser.Row, args.Exception.Message + " Column Name: '" + GetColumnName(context.Reader) + "' Column Value: '" + GetColumnValue(context.Reader) + "'");
}
else
{
AddRowError(context.Parser.Row, string.Format("Line[{0}]: {1}", context.Parser.Row, args.Exception.StackTrace));
}
return false;
},
1)how to handle ex.Data?
I had written as args.Exception.Data. But in data I do not see any key value pair as previous.
- ex is FormatException
How to handle FormatException
答案1
得分: 0
你可以使用 args.Exception.Message
。它提供了与循环遍历 ex.Data
相同的信息(以及更多)。
在版本 2.7.1 中循环遍历 ex.Data
4: 行: '4'(基于 1)
类型: 'UserQuery+Foo'
字段索引: '0'(基于 0)
字段名称: 'Id'
字段值: 'three'
args.Exception.Message
在版本 30.0.1 中
无法执行转换。
文本: 'three'
成员名称: Id
成员类型: System.Int32
类型转换器: 'CsvHelper.TypeConversion.Int32Converter'
IReader 状态:
列数: 0
当前索引: 0
标题记录:
["Id","Name"]
IParser 状态:
字节计数: 0
字符计数: 30
行: 4
原始行: 4
计数: 2
原始记录:
three,name3
英文:
You can use args.Exception.Message
. It gives the same information (plus some) as looping through ex.Data
.
looping through ex.Data
in Version 2.7.1
4: Row: '4' (1 based)
Type: 'UserQuery+Foo'
Field Index: '0' (0 based)
Field Name: 'Id'
Field Value: 'three'
args.Exception.Message
in Version 30.0.1
The conversion cannot be performed.
Text: 'three'
MemberName: Id
MemberType: System.Int32
TypeConverter: 'CsvHelper.TypeConversion.Int32Converter'
IReader state:
ColumnCount: 0
CurrentIndex: 0
HeaderRecord:
["Id","Name"]
IParser state:
ByteCount: 0
CharCount: 30
Row: 4
RawRow: 4
Count: 2
RawRecord:
three,name3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论