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;
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('app.onerror(err)', () => {
|
|
|
|
it('should throw an error if a non-error is given', done => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-24 16:24:38 +00:00
|
|
|
(() => app.onerror('foo')).should.throw(AssertionError, {message: 'non-error thrown: foo'});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should do nothing if status is 404', 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;
|
|
|
|
|
2015-10-24 16:24:38 +00:00
|
|
|
const output = stderr.inspectSync(() => app.onerror(err));
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
output.should.eql([]);
|
|
|
|
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should do nothing if .silent', 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();
|
|
|
|
|
2015-10-24 16:24:38 +00:00
|
|
|
const output = stderr.inspectSync(() => app.onerror(err));
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
output.should.eql([]);
|
|
|
|
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should log the error to stderr', 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';
|
|
|
|
|
2015-10-24 16:24:38 +00:00
|
|
|
const output = stderr.inspectSync(() => app.onerror(err));
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
output.should.eql(['\n', ' Foo\n', '\n']);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should use err.toString() instad of err.stack', 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;
|
|
|
|
|
2015-10-24 16:24:38 +00:00
|
|
|
const output = stderr.inspectSync(() => app.onerror(err));
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
output.should.eql(['\n', ' Error: mock stack null\n', '\n']);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
done();
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|