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
Add CSV::InvalidEncodingError #287
Conversation
@@ -854,6 +854,13 @@ def initialize(message, line_number) | |||
end | |||
end | |||
|
|||
# The error thrown when the parser encounters invalid encoding in CSV. | |||
class InvalidEncodingError < MalformedCSVError | |||
def initialize(encoding, line_number) |
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.
How about adding a reader for the target encoding?
def initialize(encoding, line_number) | |
attr_reader :encoding | |
def initialize(encoding, line_number) | |
@encoding = encoding |
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.
We can't get the target encoding if we don't set it to @encoding
. (See the above suggestion.)
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.
oops!
cb0a680
to
f2369db
Compare
@@ -854,6 +854,13 @@ def initialize(message, line_number) | |||
end | |||
end | |||
|
|||
# The error thrown when the parser encounters invalid encoding in CSV. | |||
class InvalidEncodingError < MalformedCSVError | |||
def initialize(encoding, line_number) |
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.
We can't get the target encoding if we don't set it to @encoding
. (See the above suggestion.)
@@ -113,7 +113,7 @@ def test_open_encoding_invalid | |||
file << "\u{1F600},\u{1F601}" | |||
end | |||
CSV.open(@input.path, encoding: "EUC-JP") do |csv| | |||
error = assert_raise(CSV::MalformedCSVError) do | |||
error = assert_raise(CSV::InvalidEncodingError) do |
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.
Could you also change the below assert_equal(...)
to the following?
assert_equal([Encoding::EUC_JP, "Invalid byte sequence in EUC-JP in line 1."],
[error.encoding, error.message])
@@ -280,7 +280,7 @@ def test_row_separator_detection_with_invalid_encoding | |||
def test_invalid_encoding_row_error | |||
csv = CSV.new("valid,x\rinvalid,\xF8\r".force_encoding("UTF-8"), | |||
encoding: "UTF-8", row_sep: "\r") | |||
error = assert_raise(CSV::MalformedCSVError) do | |||
error = assert_raise(CSV::InvalidEncodingError) do |
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.
ditto.
To handle encoding errors in CSV parsing with the appropriate error class
f2369db
to
0aa7d2f
Compare
Thanks! |
Do you want a new version soon? |
No I don't, thanks. |
(ruby/csv#287) To handle encoding errors in CSV parsing with the appropriate error class ruby/csv@68b44887e5
To handle encoding errors in CSV parsing with the appropriate error class