diff --git a/lib/context.js b/lib/context.js index 02b85d5..48abfee 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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]; diff --git a/test/context/onerror.js b/test/context/onerror.js index 6bf9c85..69c14b6 100644 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -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); + }) + }) + }) })