diff options
author | Eric Hodel <drbrain@segment7.net> | 2012-10-22 13:45:48 -0700 |
---|---|---|
committer | Eric Hodel <drbrain@segment7.net> | 2012-10-22 13:45:48 -0700 |
commit | 66e22be9d7e33eb79b204a7d55ebc5abd5284e4d (patch) | |
tree | 045a66f796d045b9cb55d700a1b71d28a36214df | |
parent | 99f60a439a75bea57a3c62135c3b020ddc93360b (diff) | |
download | psych-66e22be9d7e33eb79b204a7d55ebc5abd5284e4d.tar.gz |
Psych::SyntaxError now inherits from RuntimeError
Previously Psych::SyntaxError inherited from SyntaxError. Since
SyntaxError does not inherit from StandardError a plain rescue could not
capture a YAML parse error. This made code that needed to handle psych
errors slightly more complex.
Psych::SyntaxError now inherits from Psych::Error (allowing room for
future expansion of psych errors that can be caught under the same
umbrella) and inherits from RuntimeError.
-rw-r--r-- | ext/psych/parser.c | 2 | ||||
-rw-r--r-- | lib/psych/syntax_error.rb | 5 | ||||
-rw-r--r-- | test/psych/test_exception.rb | 21 |
3 files changed, 26 insertions, 2 deletions
diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 0908a1b..8c65ce1 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -557,7 +557,7 @@ void Init_psych_parser() rb_define_const(cPsychParser, "UTF16BE", INT2NUM(YAML_UTF16BE_ENCODING)); rb_require("psych/syntax_error"); - ePsychSyntaxError = rb_define_class_under(mPsych, "SyntaxError", rb_eSyntaxError); + ePsychSyntaxError = rb_const_get(mPsych, rb_intern("SyntaxError")); rb_define_method(cPsychParser, "parse", parse, -1); rb_define_method(cPsychParser, "mark", mark, 0); diff --git a/lib/psych/syntax_error.rb b/lib/psych/syntax_error.rb index f79743d..f972256 100644 --- a/lib/psych/syntax_error.rb +++ b/lib/psych/syntax_error.rb @@ -1,5 +1,8 @@ module Psych - class SyntaxError < ::SyntaxError + class Error < RuntimeError + end + + class SyntaxError < Error attr_reader :file, :line, :column, :offset, :problem, :context def initialize file, line, col, offset, problem, context diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb index c6d98d7..5615fc2 100644 --- a/test/psych/test_exception.rb +++ b/test/psych/test_exception.rb @@ -126,5 +126,26 @@ module Psych assert_equal 1, w.foo assert_nil w.bar end + + def test_psych_syntax_error + Tempfile.open(['parsefile', 'yml']) do |t| + t.binmode + t.write '--- `' + t.close + + begin + Psych.parse_file t.path + rescue StandardError + assert true # count assertion + ensure + return unless $! + + ancestors = $!.class.ancestors.inspect + + flunk "Psych::SyntaxError not rescued by StandardError: #{ancestors}" + end + end + end + end end |