Browse files

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.
  • Loading branch information...
1 parent 99f60a4 commit 66e22be9d7e33eb79b204a7d55ebc5abd5284e4d @drbrain drbrain committed Oct 22, 2012
Showing with 26 additions and 2 deletions.
  1. +1 −1 ext/psych/parser.c
  2. +4 −1 lib/psych/syntax_error.rb
  3. +21 −0 test/psych/test_exception.rb
View
2 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);
View
5 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
View
21 test/psych/test_exception.rb
@@ -126,5 +126,26 @@ def to_yaml_properties
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

0 comments on commit 66e22be

Please sign in to comment.