-
Notifications
You must be signed in to change notification settings - Fork 194
Make enumerateAsDict()
Accept a Throwing Function
#139
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea makes perfect sense, thanks for the PR. And sorry for the delay, this went under my radar!
I left 2 change requests at the relevant code sections.
But I couldn't leave a PR comment on unchanges lines, so: could you also rephrase the - Throws:
docstring to add that it may forward the rowCallback
error, if any, to all functions that depend on this? (You essentially found and touched all relevant parts in your PR already I believe)
Thanks!
do { | ||
try rowCallback(fields) | ||
} catch { | ||
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, this doesn't change usage because the function is throwing anyway.
But could you avoid catching, wrapping, and rethrowing the error? User code throwing a MyVerySpecificError
on failure in this callback will likely want to work with and maybe even catch MyVerySpecificError
, not CSVParseError.generic
, losing the original error's information completely here.
For context: I'm also not a fan of wrapping-and-rethrowing, because that adds another step to the debugging flow when you need to inspect the stack.
Just let rowCallback's error propagate will do The Right Thing in more cases 👍
do { | ||
try rowCallback(fields) | ||
} catch { | ||
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above here, please:
do { | |
try rowCallback(fields) | |
} catch { | |
throw CSVParseError.generic(message: "Error thrown by block for row \(rowIndex)") | |
} | |
try rowCallback(fields) |
Parser.enumerateAsArray()
catches errors and throwsCSVParseError.generic
error indicated the row on which the block threw.As far as I can tell, there should be no impact on existing code that passes a non-throwing function to
enumerateAsDict()
.