diff options
| author | Kosuke Shibata <56685224+shibaaaa@users.noreply.github.com> | 2023-09-14 09:25:06 +0900 |
|---|---|---|
| committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2023-10-03 16:13:18 +0900 |
| commit | 2325e1cd81d721645ad600d93829604c13f6cbd8 (patch) | |
| tree | f4b64f24b56240330d5ff2520dd3354fbd344a34 /lib | |
| parent | 457971f4e243a76084d06ae840f4218b7a2062a3 (diff) | |
[ruby/csv] Add CSV::InvalidEncodingError
(https://github.com/ruby/csv/pull/287)
To handle encoding errors in CSV parsing with the appropriate error
class
https://github.com/ruby/csv/commit/68b44887e5
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/csv.rb | 9 | ||||
| -rw-r--r-- | lib/csv/parser.rb | 6 |
2 files changed, 11 insertions, 4 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index d4dc569b83..81fa99aa99 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -854,6 +854,15 @@ class CSV end end + # The error thrown when the parser encounters invalid encoding in CSV. + class InvalidEncodingError < MalformedCSVError + attr_reader :encoding + def initialize(encoding, line_number) + @encoding = encoding + 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 diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb index ed9297fe30..4da87fbac8 100644 --- a/lib/csv/parser.rb +++ b/lib/csv/parser.rb @@ -414,8 +414,7 @@ class CSV 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 @@ -876,8 +875,7 @@ class CSV !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) |
