处理CSVhelper中的多类型转换异常

huangapple go评论114阅读模式
英文:

How handle Multiple Type convertor exception in CSVhelper

问题

最近我将CSVHelper库从2.7.1版本升级到30版本。我已解决所有构建错误。但是在升级后,异常未被记录。

旧代码:

  1. csvReader.Configuration.ReadingExceptionCallback =
  2. (ex, row) =>
  3. {
  4. if (ex is CsvHelper.TypeConversion.CsvTypeConverterException)
  5. {
  6. foreach (DictionaryEntry error in ex.Data)
  7. {
  8. AddRowError(row.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(row) + "'");
  9. }
  10. }
  11. else if (ex is FormatException)
  12. {
  13. AddRowError(row.Row, ex.Message + " Column Name: '" + GetColumnName(row) + "' Column Value: '" + GetColumnValue(row) + "'");
  14. }
  15. else
  16. {
  17. AddRowError(row.Row, string.Format("Line[{0}]: {1}", row.Row, ex.StackTrace));
  18. }
  19. };

新代码:

  1. ReadingExceptionOccurred = args =>
  2. {
  3. var context = args.Exception.Context;
  4. if (args.Exception is CsvHelper.TypeConversion.TypeConverterException)
  5. {
  6. foreach (DictionaryEntry error in args.Exception.Data)
  7. {
  8. AddRowError(context.Parser.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(context.Reader) + "'");
  9. }
  10. }
  11. else if (args.Exception.GetType() == typeof(FormatException))
  12. {
  13. AddRowError(context.Parser.Row, args.Exception.Message + " Column Name: '" + GetColumnName(context.Reader) + "' Column Value: '" + GetColumnValue(context.Reader) + "'");
  14. }
  15. else
  16. {
  17. AddRowError(context.Parser.Row, string.Format("Line[{0}]: {1}", context.Parser.Row, args.Exception.StackTrace));
  18. }
  19. return false;
  20. };
英文:

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:

  1. csvReader.Configuration.ReadingExceptionCallback =
  2. (ex, row) =>
  3. {
  4. if (ex is CsvHelper.TypeConversion.CsvTypeConverterException)
  5. {
  6. foreach (DictionaryEntry error in ex.Data)
  7. {
  8. AddRowError(row.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(row) + "'");
  9. }
  10. }
  11. else if (ex is FormatException)
  12. {
  13. AddRowError(row.Row, ex.Message + " Column Name: '" + GetColumnName(row) + "' Column Value: '" + GetColumnValue(row) + "'");
  14. }
  15. else
  16. {
  17. AddRowError(row.Row, string.Format("Line[{0}]: {1}", row.Row, ex.StackTrace));
  18. }
  19. };

New Code:

  1. ReadingExceptionOccurred = args =>
  2. {
  3. var context = args.Exception.Context;
  4. if (args.Exception is CsvHelper.TypeConversion.TypeConverterException)
  5. {
  6. foreach (DictionaryEntry error in args.Exception.Data)
  7. {
  8. AddRowError(context.Parser.Row, error.Value.ToString() + " Column Name: '" + GetColumnName(context.Reader) + "'");
  9. }
  10. }
  11. else if (args.Exception.GetType() == typeof(FormatException))
  12. {
  13. AddRowError(context.Parser.Row, args.Exception.Message + " Column Name: '" + GetColumnName(context.Reader) + "' Column Value: '" + GetColumnValue(context.Reader) + "'");
  14. }
  15. else
  16. {
  17. AddRowError(context.Parser.Row, string.Format("Line[{0}]: {1}", context.Parser.Row, args.Exception.StackTrace));
  18. }
  19. return false;
  20. },

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.

  1. ex is FormatException
    How to handle FormatException

处理CSVhelper中的多类型转换异常

答案1

得分: 0

你可以使用 args.Exception.Message。它提供了与循环遍历 ex.Data 相同的信息(以及更多)。

在版本 2.7.1 中循环遍历 ex.Data

  1. 4: : '4'(基于 1
  2. 类型: 'UserQuery+Foo'
  3. 字段索引: '0'(基于 0
  4. 字段名称: 'Id'
  5. 字段值: 'three'

args.Exception.Message 在版本 30.0.1 中

  1. 无法执行转换。
  2. 文本: 'three'
  3. 成员名称: Id
  4. 成员类型: System.Int32
  5. 类型转换器: 'CsvHelper.TypeConversion.Int32Converter'
  6. IReader 状态:
  7. 列数: 0
  8. 当前索引: 0
  9. 标题记录:
  10. ["Id","Name"]
  11. IParser 状态:
  12. 字节计数: 0
  13. 字符计数: 30
  14. 行: 4
  15. 原始行: 4
  16. 计数: 2
  17. 原始记录:
  18. 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

  1. 4: Row: '4' (1 based)
  2. Type: 'UserQuery+Foo'
  3. Field Index: '0' (0 based)
  4. Field Name: 'Id'
  5. Field Value: 'three'

args.Exception.Message in Version 30.0.1

  1. The conversion cannot be performed.
  2. Text: 'three'
  3. MemberName: Id
  4. MemberType: System.Int32
  5. TypeConverter: 'CsvHelper.TypeConversion.Int32Converter'
  6. IReader state:
  7. ColumnCount: 0
  8. CurrentIndex: 0
  9. HeaderRecord:
  10. ["Id","Name"]
  11. IParser state:
  12. ByteCount: 0
  13. CharCount: 30
  14. Row: 4
  15. RawRow: 4
  16. Count: 2
  17. RawRecord:
  18. three,name3

huangapple
  • 本文由 发表于 2023年2月10日 13:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定