add socket error-handling. Closes #114

This commit is contained in:
TJ Holowaychuk 2013-12-17 17:37:35 -08:00
parent 0c330ffb1a
commit 1769f9c431
3 changed files with 24 additions and 2 deletions

View file

@ -97,7 +97,9 @@ app.callback = function(){
return function(req, res, next){ return function(req, res, next){
var ctx = self.createContext(req, res); var ctx = self.createContext(req, res);
co.call(ctx, gen)(next || ctx.onerror); next = next || ctx.onerror;
ctx.socket.once('error', next);
co.call(ctx, gen)(next);
} }
}; };

View file

@ -94,7 +94,7 @@ module.exports = {
// nothing we can do here other // nothing we can do here other
// than delegate to the app-level // than delegate to the app-level
// handler and log. // handler and log.
if (this.headerSent) { if (this.headerSent || !this.socket.writable) {
err.headerSent = true; err.headerSent = true;
this.app.emit('error', err, this); this.app.emit('error', err, this);
return; return;

View file

@ -5,6 +5,26 @@ var http = require('http');
var koa = require('..'); var koa = require('..');
var fs = require('fs'); var fs = require('fs');
describe('app', function(){
it('should handle socket errors', function(done){
var app = koa();
app.use(function *(next){
// triggers this.socket.writable == false
this.socket.emit('error', new Error('boom'));
});
app.on('error', function(err){
err.message.should.equal('boom');
done();
});
request(app.listen())
.get('/')
.end(function(){});
})
})
describe('app.use(fn)', function(){ describe('app.use(fn)', function(){
it('should compose middleware', function(done){ it('should compose middleware', function(done){
var app = koa(); var app = koa();