Comments (8)
Yes, this is expected behavior. It is a SyntaxError, so Psych::SyntaxError
inherits from SyntaxError
.
from psych.
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.
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.
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.
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.
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.
I'm completely convinced of how incorrect I was. Can someone create a patch?
from psych.
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)
- YAML string in exponential format cannot be loaded by YAML 1.2 parser
- Psych::BadAlias error with simple alias HOT 1
- Version 4.0.5 breaks Date deserialization HOT 1
- JSON is invalid
- Dates with five digit years throw exception HOT 1
- Prevent snakeYAML add backslash when dump multiple line string HOT 1
- YAML.dump(date) generates warnings with Rails 7.0.7+ HOT 1
- [DOC] Link fragment does not work
- YAML 1.2 compliance possibly breaking VCR HOT 3
- Add settings for alias limits, recursion, and duplicate keys HOT 8
- TypeError: allocator undefined for Nokogiri::HTML5::Document on YAML.unsafe_load_file HOT 3
- Psych 5.1.1 fails to load on JRuby 9.x HOT 10
- Singleton Classes Aren't round-trippable
- Issue between psych and mini_racer on platform x86_64-linux HOT 1
- Psych::DisallowedClass when loading fixtures HOT 3
- Strange `FrozenError` on `Psych.load` HOT 1
- Kernel.y interferes with code relying on method_missing HOT 3
- SSL issues preventing installation HOT 1
- yaml parsing is wrong when key's value has leading colon HOT 1
- Preserve quote style while changing a YAML file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from psych.