add socket error-handling. Closes #114
This commit is contained in:
parent
0c330ffb1a
commit
1769f9c431
3 changed files with 24 additions and 2 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue