Giter Site home page Giter Site logo

Exception inside block about jcsv HOT 5 OPEN

rbotafogo avatar rbotafogo commented on May 28, 2024
Exception inside block

from jcsv.

Comments (5)

rbotafogo avatar rbotafogo commented on May 28, 2024

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 [email protected]:

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

from jcsv.

jazzido avatar jazzido commented on May 28, 2024

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!

from jcsv.

rbotafogo avatar rbotafogo commented on May 28, 2024

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 [email protected]:

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

from jcsv.

rbotafogo avatar rbotafogo commented on May 28, 2024

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 [email protected]:

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

from jcsv.

jazzido avatar jazzido commented on May 28, 2024

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

from jcsv.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.