2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const stderr = require('test-console').stderr;
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-12 04:59:30 +00:00
|
|
|
const AssertionError = require('assert').AssertionError;
|
|
|
|
|
|
|
|
describe('app.onerror(err)', function(){
|
|
|
|
it('should throw an error if a non-error is given', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
app.onerror('foo');
|
|
|
|
|
|
|
|
should.fail();
|
|
|
|
} catch (err) {
|
|
|
|
err.should.be.instanceOf(AssertionError);
|
|
|
|
err.message.should.equal('non-error thrown: foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing if status is 404', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
const err = new Error();
|
|
|
|
|
|
|
|
err.status = 404;
|
|
|
|
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
|
|
app.onerror(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
output.should.eql([]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should do nothing if .silent', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
app.silent = true;
|
|
|
|
const err = new Error();
|
|
|
|
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
|
|
app.onerror(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
output.should.eql([]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log the error to stderr', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
app.env = 'dev';
|
|
|
|
|
|
|
|
const err = new Error();
|
|
|
|
err.stack = 'Foo';
|
|
|
|
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
|
|
app.onerror(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
output.should.eql(["\n", " Foo\n", "\n"]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should use err.toString() instad of err.stack', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
app.env = 'dev';
|
|
|
|
|
|
|
|
const err = new Error('mock stack null');
|
|
|
|
err.stack = null;
|
|
|
|
|
|
|
|
const output = stderr.inspectSync(function() {
|
|
|
|
app.onerror(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
output.should.eql(["\n", " Error: mock stack null\n", "\n"]);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
})
|