Giter Site home page Giter Site logo

Comments (8)

tenderlove avatar tenderlove commented on July 28, 2024

Yes, this is expected behavior. It is a SyntaxError, so Psych::SyntaxError inherits from SyntaxError.

from psych.

chrismcg avatar chrismcg commented on July 28, 2024

We too just tripped over this. As SyntaxError is descended from ScriptError and has siblings LoadError and NotImplementedError I'd always assumed it was for Ruby syntax errors only. I think of Psych as a library and was expecting any exceptions it raised to be caught by a rescue of StandardError.

We're handling it now anyways, and I can see your point, but we did find it a bit surprising.

from psych.

nirvdrum avatar nirvdrum commented on July 28, 2024

We just got burned by this too. We had some "rescue Exception" to catch things like this, but that wreaked havoc in a multi-threaded environment. It wasn't the best code admittedly, but we tried to do the right thing and just catch StandardError. After three days of debugging, this issue was unearthed. I went to file the issue only to see it had already been closed. While it is a form of syntactical error, I don't think it's anywhere on par with a Ruby SyntaxError. I hope this practice isn't adopted en masse by other data format libraries because you can't reasonably rescue on any class higher up the ancestry without really screwing with Ruby.

from psych.

w-A-L-L-e avatar w-A-L-L-e commented on July 28, 2024

On one of my servers I updated to rails 3.2.3 and I get all these warnings whenever I run a rake task or rails console:

wschrep@Debian-60-squeeze-64-LAMP:/var/www/trouw$ rake db:dump
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant ANY
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF8
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16LE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16BE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant ANY
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF8
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16LE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16BE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant ANY
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF8
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16LE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16BE
/usr/local/lib/ruby/1.9.1/x86_64-linux/psych.so: warning: already initialized constant UTF16BE

wschrep@Debian-60-squeeze-64-LAMP:/var/www/trouw$ ruby --version
ruby 1.9.3p125 (2012-02-16) [x86_64-linux]

wschrep@Debian-60-squeeze-64-LAMP:/var/www/trouw$ rails --version
Rails 3.2.3

wschrep@Debian-60-squeeze-64-LAMP:/var/www/trouw$ gem search psych
*** LOCAL GEMS ***
psych (1.3.1)

from psych.

senorprogrammer avatar senorprogrammer commented on July 28, 2024

I agree that this is an incorrect use of SyntaxError. The literal description of a SyntaxError is "Raised when encountering Ruby code with an invalid syntax". What Psych is catching is malformed YAML. Different things.

from psych.

hynkle avatar hynkle commented on July 28, 2024

I completely agree with the other commenters saying that Ruby's built-in SyntaxError class is for Ruby code, not for just anything you might be trying to parse. This is evinced by the fact that SyntaxError is a subclass of ScriptError. My YAML is not a script—it is markup, despite having a somewhat chummier relationship with Ruby than other markup formats like JSON or XML. In your view, @tenderlove, is it incorrect to have one's parsing library's parsing exceptions subclass StandardError rather than SyntaxError, or is it just that you don't see any reason not to use SyntaxError since it's there and has such a convenient name?

I've always coded by the rule of thumb that any exception classes I create or exceptions I explicitly raise should subclass StandardError, and that exceptions not descending from StandardError are reserved for the interpreter's use. I haven't noticed anything in any libraries that I use that run counter to that guideline except for Psych::SyntaxError.

It's nice that Psych's parsing exceptions can indeed be explicitly rescued with rescue Psych::SyntaxError or more generally with a rescue Exception, but if I ever have a catch-all rescue (for exception logging or something of that nature), I absolutely do not want to be rescuing the likes of NoMemoryError, SystemExit, SystemStackError, and all the other low-level friends that would be netted by something as general as rescue Exception.

from psych.

tenderlove avatar tenderlove commented on July 28, 2024

I'm completely convinced of how incorrect I was. Can someone create a patch?

from psych.

sodabrew avatar sodabrew commented on July 28, 2024

For anyone bit by this issue, it was fixed in commit 66e22be from October 2012. The current rubygems.org release of psych 1.3.4 does not have this fix. @tenderlove Do you know at what point this change was picked up by Ruby 1.9 and/or 2.0?

from psych.

Related Issues (20)

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.