Skip to content

Commit 7a31b99

Browse files
committed
Handle lexer errors.
Fixes #105.
1 parent 22fd4be commit 7a31b99

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

lib/N3Lexer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ N3Lexer.prototype = {
410410
if (self._input !== null)
411411
self._tokenizeToEnd(callback, true);
412412
});
413+
input.on('error', callback);
413414
}
414415
},
415416
};

lib/N3StreamParser.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ function N3StreamParser(options) {
1414

1515
// Set up parser
1616
var self = this, parser = new N3Parser(options), onData, onEnd;
17-
parser.parse(
18-
// Pass dummy stream to obtain `data` and `end` callbacks
19-
{ on: function (event, cb) { event === 'data' ? (onData = cb) : (onEnd = cb); } },
20-
// Handle triples by pushing them down the pipeline
21-
function (error, t) { error && self.emit('error', error) || t && self.push(t); },
22-
// Emit prefixes through the `prefix` event
23-
function (prefix, uri) { self.emit('prefix', prefix, uri); });
17+
// Pass dummy stream to obtain `data` and `end` callbacks
18+
parser.parse({
19+
on: function (event, cb) {
20+
switch (event) {
21+
case 'data': onData = cb; break;
22+
case 'end': onEnd = cb; break;
23+
}
24+
},
25+
},
26+
// Handle triples by pushing them down the pipeline
27+
function (error, t) { error && self.emit('error', error) || t && self.push(t); },
28+
// Emit prefixes through the `prefix` event
29+
function (prefix, uri) { self.emit('prefix', prefix, uri); });
2430

2531
// Implement Transform methods through parser callbacks
2632
this._transform = function (chunk, encoding, done) { onData(chunk); done(); };

test/N3Lexer-test.js

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -730,30 +730,74 @@ describe('N3Lexer', function () {
730730
});
731731

732732
describe('passing data after the stream has been finished', function () {
733-
var tokens = [], stream = new EventEmitter(), lexer = new N3Lexer();
734-
lexer.tokenize(stream, function (error, token) { !error && tokens.push(token); });
735-
stream.emit('data', '<a> ');
736-
stream.emit('end');
737-
stream.emit('data', '<b> ');
738-
stream.emit('end');
733+
var tokens = [], error;
734+
before(function () {
735+
var stream = new EventEmitter(), lexer = new N3Lexer();
736+
lexer.tokenize(stream, function (err, token) {
737+
if (err)
738+
error = err;
739+
else
740+
tokens.push(token);
741+
});
742+
stream.emit('data', '<a> ');
743+
stream.emit('end');
744+
stream.emit('data', '<b> ');
745+
stream.emit('end');
746+
});
739747

740748
it('parses only the first chunk (plus EOF)', function () {
741749
tokens.should.have.length(2);
742750
});
751+
752+
it('does not emit an error', function () {
753+
expect(error).to.not.exist;
754+
});
743755
});
744756

745-
describe('passing data after an error has occurred', function () {
746-
var tokens = [], stream = new EventEmitter(), lexer = new N3Lexer();
747-
lexer.tokenize(stream, function (error, token) { !error && tokens.push(token); });
748-
stream.emit('data', '<a> ');
749-
stream.emit('data', ' error ');
750-
stream.emit('end');
751-
stream.emit('data', '<b> ');
752-
stream.emit('end');
757+
describe('passing data after a syntax error', function () {
758+
var tokens = [], error;
759+
before(function () {
760+
var stream = new EventEmitter(), lexer = new N3Lexer();
761+
lexer.tokenize(stream, function (err, token) {
762+
if (err)
763+
error = err;
764+
else
765+
tokens.push(token);
766+
});
767+
stream.emit('data', '<a> ');
768+
stream.emit('data', ' error ');
769+
stream.emit('end');
770+
stream.emit('data', '<b> ');
771+
stream.emit('end');
772+
});
753773

754774
it('parses only the first chunk', function () {
755775
tokens.should.have.length(1);
756776
});
777+
778+
it('emits the error', function () {
779+
expect(error).to.exist;
780+
expect(error).to.have.property('message', 'Unexpected "error" on line 1.');
781+
});
782+
});
783+
784+
describe('when the stream errors', function () {
785+
var tokens = [], error;
786+
before(function () {
787+
var stream = new EventEmitter(), lexer = new N3Lexer();
788+
lexer.tokenize(stream, function (err, token) {
789+
if (err)
790+
error = err;
791+
else
792+
tokens.push(token);
793+
});
794+
stream.emit('error', new Error('my error'));
795+
});
796+
797+
it('passes the error', function () {
798+
expect(error).to.exist;
799+
expect(error).to.have.property('message', 'my error');
800+
});
757801
});
758802

759803
describe('called with a string and without callback', function () {

0 commit comments

Comments
 (0)