catch async exceptions and emit error

This commit is contained in:
Luke Page 2015-08-08 06:53:15 +01:00
parent 3ccd332f5a
commit 6cf6d787bc
3 changed files with 82 additions and 72 deletions

View file

@ -89,7 +89,6 @@ function bitRetriever(data, depth) {
}, },
end: function() { end: function() {
if (i !== data.length) { if (i !== data.length) {
// todo these exceptions should be emitting errors
throw new Error('extra data found'); throw new Error('extra data found');
} }
} }

View file

@ -117,16 +117,7 @@ ChunkStream.prototype.destroy = function() {
this.emit('close'); this.emit('close');
}; };
ChunkStream.prototype._process = function() { ChunkStream.prototype._processReadAllowingLess = function(read) {
// as long as there is any data and read requests
while (this._buffered > 0 && this._reads && this._reads.length > 0) {
var read = this._reads[0];
// read any data (but no more than length)
if (read.allowLess) {
// ok there is any data so that we can satisfy this request // ok there is any data so that we can satisfy this request
this._reads.shift(); // == read this._reads.shift(); // == read
@ -149,11 +140,9 @@ ChunkStream.prototype._process = function() {
read.func.call(this, smallerBuf); read.func.call(this, smallerBuf);
} }
};
} ChunkStream.prototype._processRead = function(read) {
else if (this._buffered >= read.length) {
// ok we can meet some expectations
this._reads.shift(); // == read this._reads.shift(); // == read
var pos = 0; var pos = 0;
@ -183,7 +172,25 @@ ChunkStream.prototype._process = function() {
this._buffered -= read.length; this._buffered -= read.length;
read.func.call(this, data); read.func.call(this, data);
};
ChunkStream.prototype._process = function() {
try {
// as long as there is any data and read requests
while (this._buffered > 0 && this._reads && this._reads.length > 0) {
var read = this._reads[0];
// read any data (but no more than length)
if (read.allowLess) {
this._processReadAllowingLess(read);
}
else if (this._buffered >= read.length) {
// ok we can meet some expectations
this._processRead(read);
} }
else { else {
// not enought data to satisfy first request in queue // not enought data to satisfy first request in queue
@ -195,4 +202,8 @@ ChunkStream.prototype._process = function() {
if (this._buffers && this._buffers.length > 0 && this._buffers[0] == null) { if (this._buffers && this._buffers.length > 0 && this._buffers[0] == null) {
this._end(); this._end();
} }
}
catch(ex) {
this.emit('error', ex);
}
}; };

View file

@ -35,7 +35,7 @@
"coverage": "istanbul -- cover node_modules/tape/bin/tape test/*-spec.js nolarge", "coverage": "istanbul -- cover node_modules/tape/bin/tape test/*-spec.js nolarge",
"coverage-report": "npm run coverage && istanbul report html", "coverage-report": "npm run coverage && istanbul report html",
"coveralls": "cat ./coverage/lcov.info | coveralls", "coveralls": "cat ./coverage/lcov.info | coveralls",
"test": "tape test/*-spec.js | tap-dot && node test/run-compare", "test": "npm run lint && tape test/*-spec.js | tap-dot && node test/run-compare",
"lint": "eslint lib" "lint": "eslint lib"
}, },
"repository": { "repository": {