fix err.status invalid lead to uncaughtException

This commit is contained in:
dead_horse 2014-08-06 21:10:52 +08:00
parent 19a9ef76e5
commit e2f61595b8
2 changed files with 44 additions and 1 deletions

View file

@ -115,7 +115,7 @@ var proto = module.exports = {
if ('ENOENT' == err.code) err.status = 404;
// default to 500
err.status = err.status || 500;
if ('number' != typeof err.status || !http.STATUS_CODES[err.status]) err.status = 500;
// respond
var code = http.STATUS_CODES[err.status];

View file

@ -50,4 +50,47 @@ describe('ctx.onerror(err)', function(){
done();
})
})
describe('when invalid err.status', function(){
describe('not number', function(){
it('should respond 500', function(done){
var app = koa();
app.use(function *(next){
this.body = 'something else';
var err = new Error('some error');
err.status = 'notnumber';
this.throw(err);
})
var server = app.listen();
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
})
})
describe('not http status code', function(){
it('should respond 500', function(done){
var app = koa();
app.use(function *(next){
this.body = 'something else';
var err = new Error('some error');
err.status = 9999;
this.throw(err);
})
var server = app.listen();
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
})
})
})
})