Skip to content

Commit

Permalink
Add CSV::InvalidEncodingError
Browse files Browse the repository at this point in the history
To handle encoding errors in CSV parsing with the appropriate error class
  • Loading branch information
shibaaaa committed Sep 13, 2023
1 parent 281b1a5 commit f2369db
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,14 @@ def initialize(message, line_number)
end
end

# The error thrown when the parser encounters invalid encoding in CSV.
class InvalidEncodingError < MalformedCSVError
attr_reader :encoding
def initialize(encoding, line_number)
super("Invalid byte sequence in #{encoding}", line_number)
end
end

#
# A FieldInfo Struct contains details about a field's position in the data
# source it was read from. CSV will pass this Struct to some blocks that make
Expand Down
6 changes: 2 additions & 4 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ def parse(&block)
else
lineno = @lineno + 1
end
message = "Invalid byte sequence in #{@encoding}"
raise MalformedCSVError.new(message, lineno)
raise InvalidEncodingError.new(@encoding, lineno)
rescue UnexpectedError => error
if @scanner
ignore_broken_line
Expand Down Expand Up @@ -876,8 +875,7 @@ def build_scanner
!line.valid_encoding?
end
if index
message = "Invalid byte sequence in #{@encoding}"
raise MalformedCSVError.new(message, @lineno + index + 1)
raise InvalidEncodingError.new(@encoding, @lineno + index + 1)
end
end
Scanner.new(string)
Expand Down
2 changes: 1 addition & 1 deletion test/csv/interface/test_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
csv.shift
end
assert_equal("Invalid byte sequence in EUC-JP in line 1.",
Expand Down
2 changes: 1 addition & 1 deletion test/csv/test_encodings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
csv.shift
csv.shift
end
Expand Down

0 comments on commit f2369db

Please sign in to comment.