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

Add CSV::InvalidEncodingError #287

Merged
merged 1 commit into from Sep 14, 2023

Conversation

shibaaaa
Copy link
Contributor

To handle encoding errors in CSV parsing with the appropriate error class

@@ -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)
Copy link
Member

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?

Suggested change
def initialize(encoding, line_number)
attr_reader :encoding
def initialize(encoding, line_number)
@encoding = encoding

Copy link
Member

@kou kou Sep 13, 2023

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.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops!

@shibaaaa shibaaaa force-pushed the add-csv-invalid-encoding-error branch from cb0a680 to f2369db Compare September 13, 2023 09:03
@shibaaaa shibaaaa requested a review from kou September 13, 2023 09:04
@@ -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)
Copy link
Member

@kou kou Sep 13, 2023

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
Copy link
Member

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
Copy link
Member

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
@shibaaaa shibaaaa force-pushed the add-csv-invalid-encoding-error branch from f2369db to 0aa7d2f Compare September 13, 2023 22:18
@shibaaaa shibaaaa requested a review from kou September 13, 2023 22:18
@kou kou merged commit 68b4488 into ruby:master Sep 14, 2023
37 checks passed
@kou
Copy link
Member

kou commented Sep 14, 2023

Thanks!

@kou
Copy link
Member

kou commented Sep 14, 2023

Do you want a new version soon?

@shibaaaa
Copy link
Contributor Author

@kou

Do you want a new version soon?

No I don't, thanks.

@shibaaaa shibaaaa deleted the add-csv-invalid-encoding-error branch September 14, 2023 01:06
matzbot pushed a commit to ruby/ruby that referenced this pull request Oct 3, 2023
(ruby/csv#287)

To handle encoding errors in CSV parsing with the appropriate error
class

ruby/csv@68b44887e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants