fix err.status invalid lead to uncaughtException
This commit is contained in:
parent
19a9ef76e5
commit
e2f61595b8
2 changed files with 44 additions and 1 deletions
|
@ -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];
|
||||
|
|
|
@ -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);
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue