Skip to content
Browse files

xml syntax errors are now Error objects

integrates better with existing error handling and stack printing
  • Loading branch information...
1 parent 14238a5 commit 668b93ab247c89c4f680d36008c17d34bec22e7f @defunctzombie defunctzombie committed
Showing with 31 additions and 20 deletions.
  1. +5 −2 src/xml_syntax_error.cc
  2. +18 −8 test/html_parser.js
  3. +8 −10 test/xml_parser.js
View
7 src/xml_syntax_error.cc
@@ -25,7 +25,10 @@ namespace libxmljs {
v8::Handle<v8::Value>
XmlSyntaxError::BuildSyntaxError(xmlError* error) {
- v8::Local<v8::Object> out = v8::Object::New();
+ v8::Local<v8::Value> err = v8::Exception::Error(
+ v8::String::New(error->message));
+ v8::Local<v8::Object> out = v8::Local<v8::Object>::Cast(err);
+
set_numeric_field(out, "domain", error->domain);
set_numeric_field(out, "code", error->code);
set_string_field(out, "message", error->message);
@@ -41,7 +44,7 @@ XmlSyntaxError::BuildSyntaxError(xmlError* error) {
if (error->int1) {
set_numeric_field(out, "int1", error->int1);
}
- return out;
+ return err;
}
void
View
26 test/html_parser.js
@@ -1,6 +1,16 @@
var fs = require('fs');
var libxml = require('../index');
+function make_error(object) {
+ var err = new Error(object.message);
+ err.domain = object.domain;
+ err.code = object.code;
+ err.level = object.level;
+ err.line = object.line;
+ err.column = object.column;
+ return err;
+}
+
module.exports.parse = function(assert) {
var filename = __dirname + '/fixtures/parser.html';
var str = fs.readFileSync(filename, 'utf8');
@@ -16,30 +26,30 @@ module.exports.recoverable_parse = function(assert) {
var recoverableFile = __dirname +'/fixtures/warnings/amp.html';
var str = fs.readFileSync(recoverableFile, 'utf8');
var recoverableErrors = [
- { domain: 5,
+ make_error({ domain: 5,
code: 23,
message: "htmlParseEntityRef: expecting ';'\n",
level: 2,
line: 12,
- column: 27 },
- { domain: 5,
+ column: 27 }),
+ make_error({ domain: 5,
code: 68,
message: "htmlParseEntityRef: no name\n",
level: 2,
line: 12,
- column: 38 },
- { domain: 5,
+ column: 38 }),
+ make_error({ domain: 5,
code: 23,
message: "htmlParseEntityRef: expecting ';'\n",
level: 2,
line: 14,
- column: 4 },
- { domain: 5,
+ column: 4 }),
+ make_error({ domain: 5,
code: 68,
message: "htmlParseEntityRef: no name\n",
level: 2,
line: 15,
- column: 4 }
+ column: 4 })
];
var doc = libxml.parseHtmlString(str);
View
18 test/xml_parser.js
@@ -24,16 +24,14 @@ module.exports.recoverable_parse = function(assert) {
var str = fs.readFileSync(filename, 'utf8');
var doc = libxml.parseXmlString(str);
- var err = {
- domain: 3,
- code: 201,
- message: "Namespace prefix prefix on node is not defined\n",
- level: 2,
- line: 1,
- str1: "prefix",
- str2: "node",
- column: 13,
- };
+ var err = new Error('Namespace prefix prefix on node is not defined\n');
+ err.domain = 3;
+ err.code = 201;
+ err.level = 2;
+ err.line = 1;
+ err.str1 = 'prefix';
+ err.str2 = 'node';
+ err.column = 13;
assert.equal(1, doc.errors.length);
assert.deepEqual(err, doc.errors.shift());
assert.done();

0 comments on commit 668b93a

Please sign in to comment.
Something went wrong with that request. Please try again.