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

Exception inside block #1

Open
jazzido opened this issue Jun 24, 2016 · 5 comments
Open

Exception inside block #1

jazzido opened this issue Jun 24, 2016 · 5 comments

Comments

@jazzido
Copy link

jazzido commented Jun 24, 2016

Hi,

It seems to be impossible to catch an exception inside the block that is passed to reader#read. My code looks like this:

reader = Jcsv.reader(path,
                format: :map)
    reader.filters = { :agno => Jcsv.int, :rbd => Jcsv.int }
    reader.read do |ln, rn, row, headers|
      begin
        writer << row
      rescue
        # skip row
      end
    end

A row in my CSV contains an invalid value for the rbd column, causing jCSV to throw an exception

Jcsv::FilterError: '9089ÃSICA JOSÉ TORIBIO MEDINA' could not be parsed as an Integer in {lineNo=1576684, rowNo=1576684, columnNo=2, rowSource=[2011, 9089ÃSICA JOSÉ TORIBIO MEDINA, 13, 13120, ÑUÑOA, 1, 0, 110, 1, A, 12037904, 1, 20040909, 0, 13120, ÑUÑOA,  ,  , 20110215, 5,7, 91, P, P]}
                     execute at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/numeric_filters.rb:42
  block in executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:122
             each_with_index at uri:classloader:/jruby/java/java_ext/java.lang.rb:19
           executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:96
                filter_input at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:220
                        read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:209
                  read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:352
                  read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:184
            parse_with_block at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:101
                        read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:92

Is there anyway to skip a row that fails to validate the filters?

PS: Thanks for this gem! It's making my life a lot easier :)

@rbotafogo
Copy link
Owner

Manuel,

In this version, there is no way to skip a row that fails to validate a
filter. I might take a look to see how this can be done and come up with a
new version. Do you have a suggestion?

Cheers...

2016-06-24 18:45 GMT-03:00 Manuel Aristarán notifications@github.com:

Hi,

It seems to be impossible to catch an exception inside the block that is
passed to reader#read. My code looks like this:

reader = Jcsv.reader(path,
format: :map)
reader.filters = { :agno => Jcsv.int, :rbd => Jcsv.int }
reader.read do |ln, rn, row, headers|
begin
writer << row
rescue
# skip row
end
end

A row in my CSV contains an invalid value for in the rbd column, causing
jCSV to throw an exception

Jcsv::FilterError: '9089ÃSICA JOSÉ TORIBIO MEDINA' could not be parsed as an Integer in {lineNo=1576684, rowNo=1576684, columnNo=2, rowSource=[2011, 9089ÃSICA JOSÉ TORIBIO MEDINA, 13, 13120, ÑUÑOA, 1, 0, 110, 1, A, 12037904, 1, 20040909, 0, 13120, ÑUÑOA, , , 20110215, 5,7, 91, P, P]}
execute at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/numeric_filters.rb:42
block in executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:122
each_with_index at uri:classloader:/jruby/java/java_ext/java.lang.rb:19
executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:96
filter_input at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:220
read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:209
read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:352
read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:184
parse_with_block at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:101
read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:92

Is there anyway to skip a row that fails to validate the filters?

PS: Thanks for this gem! It's making my life a lot easier :)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1, or mute the thread
https://github.com/notifications/unsubscribe/AD0H8fmMPLboRrFYuerKCYlapfediZquks5qPE_pgaJpZM4I-Jlh
.

Rodrigo Botafogo

@jazzido
Copy link
Author

jazzido commented Jun 27, 2016

Making exceptions catchable inside the begin/end block would allow developers to implement their own logic. As it is now, there's no way to do that.

Thanks!

@rbotafogo
Copy link
Owner

Manuel,

After I send my e-mail I understood what you meant. I´m working on fixing
the raise/catch path. This is actually the syntax for Ruby since the raise
is in the block.

  begin
    reader.read do |line_no, row_no, row, headers|
      p row
    end
  rescue Jcsv::FilterError => e
    retry
  end

This should solve your problem.

2016-06-27 11:34 GMT-03:00 Manuel Aristarán notifications@github.com:

Making the exception catchable inside the begin/end block would allow
developers to implement their own logic. As it is now, there's no way to do
that.

Thanks!


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#1 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AD0H8VuFDdWAzzQE_NvibYEqHXWUy-dpks5qP99_gaJpZM4I-Jlh
.

Rodrigo Botafogo
Integrando TI ao seu negócio
21-3010-4802/11-3010-1802

@rbotafogo
Copy link
Owner

Manuel,

After working for a while, I think that actually my first answer was wrong.
There is no need to change the Gem. If you need to skip the row, then just
put you ‘begin’ clause outside of the block. So, I think that:

begin
reader.read do |line_no, row_no, row, headers|
p row
end
rescue Jcsv::FilterError => e
retry
end

This should solve your problem. Is that right?

2016-06-24 18:45 GMT-03:00 Manuel Aristarán notifications@github.com:

Hi,

It seems to be impossible to catch an exception inside the block that is
passed to reader#read. My code looks like this:

reader = Jcsv.reader(path,
format: :map)
reader.filters = { :agno => Jcsv.int, :rbd => Jcsv.int }
reader.read do |ln, rn, row, headers|
begin
writer << row
rescue
# skip row
end
end

A row in my CSV contains an invalid value for in the rbd column, causing
jCSV to throw an exception

Jcsv::FilterError: '9089ÃSICA JOSÉ TORIBIO MEDINA' could not be parsed as an Integer in {lineNo=1576684, rowNo=1576684, columnNo=2, rowSource=[2011, 9089ÃSICA JOSÉ TORIBIO MEDINA, 13, 13120, ÑUÑOA, 1, 0, 110, 1, A, 12037904, 1, 20040909, 0, 13120, ÑUÑOA, , , 20110215, 5,7, 91, P, P]}
execute at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/numeric_filters.rb:42
block in executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:122
each_with_index at uri:classloader:/jruby/java/java_ext/java.lang.rb:19
executeProcessors at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:96
filter_input at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:220
read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/supercsv_interface.rb:209
read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:352
read_chunk at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:184
parse_with_block at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/reader.rb:101
read at /home/hermes/.rvm/gems/jruby-9.0.5.0/gems/mdarray-jCSV-0.6.2-java/lib/map_reader.rb:92

Is there anyway to skip a row that fails to validate the filters?

PS: Thanks for this gem! It's making my life a lot easier :)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1, or mute the thread
https://github.com/notifications/unsubscribe/AD0H8fmMPLboRrFYuerKCYlapfediZquks5qPE_pgaJpZM4I-Jlh
.

Rodrigo Botafogo

@jazzido
Copy link
Author

jazzido commented Jun 28, 2016

Yeah, that solves the problem. However, I find it a bit strange to wrap the entire read call inside a begin/end, and having to retry in case of an error. Additionally, the rescue scope does not have access to the block arguments (line_no, row_no, row, headers).

In my opinion, this feels a bit more natural:

reader.read do |ln, rn, row, headers|
      begin
        writer << row
      rescue
        # handle exception: skip row, report error, abort, whatever.
      end
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants