diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-22 01:26:40 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-22 01:26:40 +0000 |
commit | 8dd3a4af668cd92245af86206371d3d2e9619571 (patch) | |
tree | 1f617dfc6f151f32528b463789b93645d74c487f | |
parent | 0331314d278d0590ff26eb7ec156f859b590464f (diff) | |
download | ruby-8dd3a4af668cd92245af86206371d3d2e9619571.tar.gz |
* ext/psych/parser.c (parse): add the file name to the exception when
parse errors occur.
* test/psych/test_parser.rb: test for parse error file name
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30626 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ext/psych/parser.c | 11 | ||||
-rw-r--r-- | test/psych/test_parser.rb | 17 |
3 files changed, 34 insertions, 1 deletions
@@ -1,3 +1,10 @@ +Sat Jan 22 10:25:19 2011 Aaron Patterson <aaron@tenderlovemaking.com> + + * ext/psych/parser.c (parse): add the file name to the exception when + parse errors occur. + + * test/psych/test_parser.rb: test for parse error file name + Sat Jan 22 10:12:30 2011 Aaron Patterson <aaron@tenderlovemaking.com> * ext/psych/parser.c (parse): fix assertion error when reusing a diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 5a9d2c75f9..7bfdf4af90 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -4,6 +4,7 @@ VALUE cPsychParser; VALUE ePsychSyntaxError; static ID id_read; +static ID id_path; static ID id_empty; static ID id_start_stream; static ID id_end_stream; @@ -93,13 +94,20 @@ static VALUE parse(VALUE self, VALUE yaml) while(!done) { if(!yaml_parser_parse(parser, &event)) { + VALUE path; size_t line = parser->mark.line; size_t column = parser->mark.column; + if(rb_respond_to(yaml, id_path)) + path = rb_funcall(yaml, id_path, 0); + else + path = rb_str_new2("<unknown>"); + yaml_parser_delete(parser); yaml_parser_initialize(parser); - rb_raise(ePsychSyntaxError, "couldn't parse YAML at line %d column %d", + rb_raise(ePsychSyntaxError, "(%s): couldn't parse YAML at line %d column %d", + StringValuePtr(path), (int)line, (int)column); } @@ -360,6 +368,7 @@ void Init_psych_parser() rb_define_method(cPsychParser, "external_encoding=", set_external_encoding, 1); id_read = rb_intern("read"); + id_path = rb_intern("path"); id_empty = rb_intern("empty"); id_start_stream = rb_intern("start_stream"); id_end_stream = rb_intern("end_stream"); diff --git a/test/psych/test_parser.rb b/test/psych/test_parser.rb index ca69b1a1e3..a60a0c6d86 100644 --- a/test/psych/test_parser.rb +++ b/test/psych/test_parser.rb @@ -138,6 +138,23 @@ module Psych end end + def test_syntax_error_has_path_for_string + e = assert_raises(Psych::SyntaxError) do + @parser.parse("---\n\"foo\"\n\"bar\"\n") + end + assert_match '(<unknown>):', e.message + end + + def test_syntax_error_has_path_for_io + io = StringIO.new "---\n\"foo\"\n\"bar\"\n" + def io.path; "hello!"; end + + e = assert_raises(Psych::SyntaxError) do + @parser.parse(io) + end + assert_match "(#{io.path}):", e.message + end + def test_mapping_end @parser.parse("---\n!!map { key: value }") assert_called :end_mapping |