Skip to content

2.5 --> 2.6: backwards incompatible change in MalformedCSVError#new #104

@Spakman

Description

@Spakman

In Ruby 2.5, CSV::MalformedCSVError simply inherited from RuntimeError. In 2.6, it defines its own #new, taking 2 arguments (as opposed to the single argument of RuntimeError).

Code explicitly raising new errors of this class outside of the CSV library implementation itself breaks since it doesn't include the second argument (a line number).

Is making the line number optional something you'd entertain?

Activity

kou

kou commented on Sep 2, 2019

@kou
Member

Could you show codes that creating CSV::MalformedCSVError?

Mifrill

Mifrill commented on Oct 11, 2019

@Mifrill

@kou just this:

    def raise_malformed_csv_error
      raise CSV::MalformedCSVError
    end

with 2.6 ruby it's raised this:

     Failure/Error: raise CSV::MalformedCSVError
     
     ArgumentError:
       wrong number of arguments (given 0, expected 2)

because of this:
36e1cb2

csv/lib/csv.rb

Lines 212 to 219 in 36e1cb2

class MalformedCSVError < RuntimeError
attr_reader :line_number
alias_method :lineno, :line_number
def initialize(message, line_number)
@line_number = line_number
super("#{message} in line #{line_number}.")
end
end

For Ruby 2.5 code was without required arguments:

  # The error thrown when the parser encounters illegal CSV formatting.
  class MalformedCSVError < RuntimeError; end

  #

https://bugs.ruby-lang.org/issues/16133

kou

kou commented on Oct 11, 2019

@kou
Member

Thanks.
One more question. Why do you want to raise CSV::MalformedCSVError manually?

Mifrill

Mifrill commented on Oct 11, 2019

@Mifrill

@kou well, in our specific case, we have a logic with check of valid headers

...
raise_malformed_csv_error unless valid_headers?
...

    def raise_malformed_csv_error
      raise CSV::MalformedCSVError
    end

    def valid_headers?
      has_required_columns? && has_only_supported_columns?
    end

So, I'm thinking about this error like a any error of CSV parsing, like a it's an explicit error, which was inherited from RuntimeError.

kou

kou commented on Oct 11, 2019

@kou
Member

Thanks.
It seems that it's an application specific error and it's not a general CSV error. So you should use your own error class instead of reusing CSV::MalformedCSVError.

Mifrill

Mifrill commented on Oct 11, 2019

@Mifrill

@kou okay, thanks, I will take note and we will discuss it with colleagues. We've just got this error while ruby update. Now we'got how to resolve it. Thank you

kou

kou commented on Oct 11, 2019

@kou
Member

OK. I close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Spakman@kou@Mifrill

        Issue actions

          2.5 --> 2.6: backwards incompatible change in MalformedCSVError#new · Issue #104 · ruby/csv