Merge pull request #330 from dead-horse/onerror-status

Onerror status
This commit is contained in:
TJ Holowaychuk 2014-08-07 19:04:10 -07:00
commit f9eb219ecf
3 changed files with 69 additions and 3 deletions

View file

@ -75,7 +75,7 @@ var proto = module.exports = {
var err = msg instanceof Error ? msg : new Error(msg); var err = msg instanceof Error ? msg : new Error(msg);
err.status = status || err.status || 500; err.status = status || err.status || 500;
err.expose = err.status < 500; err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500;
throw err; throw err;
}, },
@ -115,7 +115,7 @@ var proto = module.exports = {
if ('ENOENT' == err.code) err.status = 404; if ('ENOENT' == err.code) err.status = 404;
// default to 500 // default to 500
err.status = err.status || 500; if ('number' != typeof err.status || !http.STATUS_CODES[err.status]) err.status = 500;
// respond // respond
var code = http.STATUS_CODES[err.status]; var code = http.STATUS_CODES[err.status];

View file

@ -50,4 +50,48 @@ describe('ctx.onerror(err)', function(){
done(); 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';
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;
throw err;
})
var server = app.listen();
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
})
})
})
}) })

View file

@ -10,7 +10,7 @@ describe('ctx.throw(msg)', function(){
ctx.throw('boom'); ctx.throw('boom');
} catch (err) { } catch (err) {
assert(500 == err.status); assert(500 == err.status);
assert(false === err.expose); assert(!err.expose);
done(); done();
} }
}) })
@ -26,6 +26,7 @@ describe('ctx.throw(err)', function(){
} catch (err) { } catch (err) {
assert(500 == err.status); assert(500 == err.status);
assert('test' == err.message); assert('test' == err.message);
assert(!err.expose);
done(); done();
} }
}) })
@ -41,6 +42,7 @@ describe('ctx.throw(err, status)', function(){
} catch (err) { } catch (err) {
assert(422 == err.status); assert(422 == err.status);
assert('test' == err.message); assert('test' == err.message);
assert(true === err.expose);
done(); done();
} }
}) })
@ -56,6 +58,7 @@ describe('ctx.throw(status, err)', function(){
} catch (err) { } catch (err) {
assert(422 == err.status); assert(422 == err.status);
assert('test' == err.message); assert('test' == err.message);
assert(true === err.expose);
done(); done();
} }
}) })
@ -70,6 +73,7 @@ describe('ctx.throw(msg, status)', function(){
} catch (err) { } catch (err) {
assert('name required' == err.message); assert('name required' == err.message);
assert(400 == err.status); assert(400 == err.status);
assert(true === err.expose);
done(); done();
} }
}) })
@ -84,6 +88,7 @@ describe('ctx.throw(status, msg)', function(){
} catch (err) { } catch (err) {
assert('name required' == err.message); assert('name required' == err.message);
assert(400 == err.status); assert(400 == err.status);
assert(true === err.expose);
done(); done();
} }
}) })
@ -98,7 +103,24 @@ describe('ctx.throw(status)', function(){
} catch (err) { } catch (err) {
assert('Bad Request' == err.message); assert('Bad Request' == err.message);
assert(400 == err.status); assert(400 == err.status);
assert(true === err.expose);
done(); done();
} }
}) })
describe('when not valid status', function(){
it('should not expose', function(done){
var ctx = context();
try {
var err = new Error('some error');
err.status = -1;
ctx.throw(err);
} catch(err) {
assert('some error' == err.message);
assert(!err.expose);
done();
}
})
})
}) })