pguardiario
pguardiario

Reputation: 55002

Problems catching unidecoder exceptions

I'm trying out the unidecoder gem and it's giving me problems with some strings:

require 'unidecoder'
str = "\u00A3"
str.to_ascii

#: (C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml): found unknown escape character while parsing a quote d scalar at line 2 column 3 from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:in parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:203:inparse_stream' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:151:in parse' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:127:inload' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in block in load_file' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:inopen' from C:/Ruby193/lib/ruby/1.9.1/psych.rb:297:in load_file' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:8:in block in ' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in yield' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in default' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:78:in decode_char' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:39:in block in decode' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in gsub' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:37:in decode' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder.rb:16:in to_ascii' from (irb):21 from C:/Ruby193/bin/irb:12:in'>>

What's worse, I can't catch the error by doing:

foo = str.to_ascii rescue 'x'

Does anyone know what's happening here?

Upvotes: 0

Views: 566

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

rescue clause with no parameter list, the parameter defaults to StandardError; it looks like unidecoder raises kinda other exception, but the stacktrace seems to be incomplete (it should show the exception type.)

Upvotes: 1

Arie Xiao
Arie Xiao

Reputation: 14082

Take a look at "C:/Ruby193/lib/ruby/gems/1.9.1/gems/unidecoder-1.1.1/lib/unidecoder/data/x00.yml". Line 2 is an YAML entry - "\z",which is not a valid escape sequence in Ruby(but a Regexp anchor to mark the end of string). This might be a bug. You can edit this line to - "\x00".

However, "\u00A3"(£) is not a valid ASCII character, I didn't find the point of encoding it to ASCII.

The exception raised is Psych::SyntaxError, you can catch that specific exception, as @mudasobwa commented.

Upvotes: 1

Related Questions