Skip to content

Commit

Permalink
Merge pull request koajs#330 from dead-horse/onerror-status
Browse files Browse the repository at this point in the history
Onerror status
  • Loading branch information
tj committed Aug 8, 2014
2 parents 2d197a7 + fa5948c commit f9eb219
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var proto = module.exports = {

var err = msg instanceof Error ? msg : new Error(msg);
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;
},

Expand Down Expand Up @@ -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];
Expand Down
44 changes: 44 additions & 0 deletions test/context/onerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,48 @@ 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';
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);
})
})
})
})
24 changes: 23 additions & 1 deletion test/context/throw.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('ctx.throw(msg)', function(){
ctx.throw('boom');
} catch (err) {
assert(500 == err.status);
assert(false === err.expose);
assert(!err.expose);
done();
}
})
Expand All @@ -26,6 +26,7 @@ describe('ctx.throw(err)', function(){
} catch (err) {
assert(500 == err.status);
assert('test' == err.message);
assert(!err.expose);
done();
}
})
Expand All @@ -41,6 +42,7 @@ describe('ctx.throw(err, status)', function(){
} catch (err) {
assert(422 == err.status);
assert('test' == err.message);
assert(true === err.expose);
done();
}
})
Expand All @@ -56,6 +58,7 @@ describe('ctx.throw(status, err)', function(){
} catch (err) {
assert(422 == err.status);
assert('test' == err.message);
assert(true === err.expose);
done();
}
})
Expand All @@ -70,6 +73,7 @@ describe('ctx.throw(msg, status)', function(){
} catch (err) {
assert('name required' == err.message);
assert(400 == err.status);
assert(true === err.expose);
done();
}
})
Expand All @@ -84,6 +88,7 @@ describe('ctx.throw(status, msg)', function(){
} catch (err) {
assert('name required' == err.message);
assert(400 == err.status);
assert(true === err.expose);
done();
}
})
Expand All @@ -98,7 +103,24 @@ describe('ctx.throw(status)', function(){
} catch (err) {
assert('Bad Request' == err.message);
assert(400 == err.status);
assert(true === err.expose);
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();
}
})
})
})

0 comments on commit f9eb219

Please sign in to comment.