Created
May 13, 2014 03:27
-
-
Save oconnore/8d9cda5e30bb55371c2f to your computer and use it in GitHub Desktop.
Why is the last error unhandled?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var s = require('stream'); | |
var mys; | |
/* ======================================================== | |
Error callback first, throw | |
======================================================== */ | |
mys = Object.create(s.Readable.prototype); | |
mys._read = function(size) { | |
this.emit('error', new Error('test')); | |
} | |
s.Readable.call(mys, { | |
objectMode: true | |
}); | |
mys.on('error', function(err) { | |
console.log('error on', err); | |
}); | |
mys.on('data', function(x) { | |
console.log('data received', x); | |
}); | |
mys.on('end', function() { | |
console.log('end received', x); | |
}); | |
/* ======================================================== | |
Error callback first, data | |
======================================================== */ | |
console.log('==========================='); | |
mys = Object.create(s.Readable.prototype); | |
mys._read = function(size) { | |
this.push('hello'); | |
this.push(null); | |
} | |
s.Readable.call(mys, { | |
objectMode: true | |
}); | |
mys.on('error', function(err) { | |
console.log('error on', err); | |
}); | |
mys.on('data', function(x) { | |
console.log('data received', x); | |
}); | |
mys.on('end', function() { | |
console.log('end received'); | |
}); | |
/* ======================================================== | |
Error callback last, data | |
======================================================== */ | |
console.log('==========================='); | |
mys = Object.create(s.Readable.prototype); | |
mys._read = function(size) { | |
this.push('hello'); | |
this.push(null); | |
} | |
s.Readable.call(mys, { | |
objectMode: true | |
}); | |
mys.on('data', function(x) { | |
console.log('data received', x); | |
}); | |
mys.on('end', function() { | |
console.log('end received'); | |
}); | |
mys.on('error', function(err) { | |
console.log('error on', err); | |
}); | |
/* ======================================================== | |
Error callback last, throw | |
======================================================== */ | |
console.log('==========================='); | |
mys = Object.create(s.Readable.prototype); | |
mys._read = function(size) { | |
this.emit('error', new Error('test')); | |
} | |
s.Readable.call(mys, { | |
objectMode: true | |
}); | |
mys.on('data', function(x) { | |
console.log('data received', x); | |
}); | |
mys.on('end', function() { | |
console.log('end received', x); | |
}); | |
mys.on('error', function(err) { | |
console.log('error on', err); | |
}); |
To add, as you probably have already noticed it's not just that it's bound last, but rather that it is bound after data. Removing the data event binding for the final attempt allows this to function without error. It doesn't actually catch the error though... Curious to find out what this is about, good luck! https://gist.github.com/MattSurabian/d88a8fd249dd3702b6c4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find it particularly strange that this outputs the same error with the same line number: https://gist.github.com/MattSurabian/db1fe15b0d90af230caa