Skip to content

Commit

Permalink
add syntax error node path
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanong committed Oct 15, 2024
1 parent 0df9227 commit d524c8d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ext/nokogiri/xml_syntax_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ noko__error_raise(void *ctx, xmlErrorConstPtr error)
VALUE
noko_xml_syntax_error__wrap(xmlErrorConstPtr error)
{
xmlChar *c_path ;
VALUE msg, e, klass;

klass = cNokogiriXmlSyntaxError;
Expand All @@ -61,16 +62,21 @@ noko_xml_syntax_error__wrap(xmlErrorConstPtr error)
);

if (error) {
c_path = xmlGetNodePath(error->node);

rb_iv_set(e, "@domain", INT2NUM(error->domain));
rb_iv_set(e, "@code", INT2NUM(error->code));
rb_iv_set(e, "@level", INT2NUM((short)error->level));
rb_iv_set(e, "@file", RBSTR_OR_QNIL(error->file));
rb_iv_set(e, "@line", INT2NUM(error->line));
rb_iv_set(e, "@path", RBSTR_OR_QNIL(c_path));
rb_iv_set(e, "@str1", RBSTR_OR_QNIL(error->str1));
rb_iv_set(e, "@str2", RBSTR_OR_QNIL(error->str2));
rb_iv_set(e, "@str3", RBSTR_OR_QNIL(error->str3));
rb_iv_set(e, "@int1", INT2NUM(error->int1));
rb_iv_set(e, "@column", INT2NUM(error->int2));

xmlFree(c_path);
}

return e;
Expand Down
9 changes: 9 additions & 0 deletions lib/nokogiri/xml/syntax_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ def aggregate(errors)
end
end

# What part of libxml2 raised this error (enum xmlErrorDomain)
attr_reader :domain
# libxml2 error code (enum xmlParserErrors)
attr_reader :code
# libxml2 error level (enum xmlErrorLevel)
attr_reader :level
attr_reader :file
attr_reader :line
# libxml2 path of the node in the tree that caused the error
attr_reader :path
# libxml2 extra string information
attr_reader :str1
# libxml2 extra string information
attr_reader :str2
# libxml2 extra string information
attr_reader :str3
# libxml2 extra extra number information
attr_reader :int1
attr_reader :column

Expand Down
4 changes: 4 additions & 0 deletions test/xml/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ class TestNokogiriXMLSchema < Nokogiri::TestCase

assert(errors = xsd.validate(doc))
assert_equal(2, errors.length)
assert_equal(
["/purchaseOrder/billTo/state", "/purchaseOrder/shipTo/state"],
errors.map(&:path).sort,
)
end

it "validate_invalid_file" do
Expand Down
1 change: 1 addition & 0 deletions test/xml/test_syntax_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
assert_nil error.column
assert_nil error.level
end
assert_nil error.path
end
end

0 comments on commit d524c8d

Please sign in to comment.