Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always provide an meaningful exception message when CSV parsing fails #269

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

loop-evgeny
Copy link

When parsing failed the exception message was previous the generic "Exception of type 'Sylvan.Data.Csv.CsvFormatException' was thrown", which gave no clue as to what was wrong. Provide a good exception message in each case where this exception is thrown, including the position where parsing failed, a snippet of the input (which can be searched for to locate the problematic input easily) and what was wrong with it. Update unit tests to check for the new exception messages.

When parsing failed the exception message was previous the generic "Exception of type 'Sylvan.Data.Csv.CsvFormatException' was thrown", which gave no clue as to what was wrong. Provide a good exception message in each case where this exception is thrown, including the position where parsing failed, a snippet of the input (which can be searched for to locate the problematic input easily) and what was wrong with it. Update unit tests to check for the new exception messages.
@@ -7,8 +7,8 @@ namespace Sylvan.Data.Csv;
/// </summary>
public class CsvFormatException : FormatException
{
internal CsvFormatException(int row, int ordinal, string? msg = null, Exception? inner = null)
: base(msg, inner)
internal CsvFormatException(int row, int ordinal, string message, Exception? inner = null)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made message mandatory (non-nullable), so that any future uses of this exception would require a message to be passed in as well.

@srs-adamr
Copy link

When parsing failed the exception message was previous the generic "Exception of type 'Sylvan.Data.Csv.CsvFormatException' was thrown", which gave no clue as to what was wrong. Provide a good exception message in each case where this exception is thrown, including the position where parsing failed, a snippet of the input (which can be searched for to locate the problematic input easily) and what was wrong with it. Update unit tests to check for the new exception messages.

I would say there may be a want to omit snippets via configuration, for sensitivity in log compliance. Should make that configurable.

@loop-evgeny
Copy link
Author

I would say there may be a want to omit snippets via configuration, for sensitivity in log compliance. Should make that configurable.

That's a good point, though it does complicate things a little. If the maintainer wants this then I think I'd rather change this PR to not include a snippet of the input at all, then try to add that in a later PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants