2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('app.onerror(err)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
global.console = jest.genMockFromModule('console');
|
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
afterEach(() => {
|
|
|
|
global.console = require('console');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if a non-error is given', () => {
|
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.throws(() => {
|
|
|
|
app.onerror('foo');
|
2018-06-06 04:55:20 +00:00
|
|
|
}, TypeError, 'non-error thrown: foo');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should do nothing if status is 404', () => {
|
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;
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
app.onerror(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(console.error.mock.calls, []);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should do nothing if .silent', () => {
|
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();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
app.onerror(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(console.error.mock.calls, []);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should log the error to stderr', () => {
|
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';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
app.onerror(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const stderr = console.error.mock.calls.join('\n');
|
|
|
|
assert.deepEqual(stderr, '\n Foo\n');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should use err.toString() instad of err.stack', () => {
|
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;
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
app.onerror(err);
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const stderr = console.error.mock.calls.join('\n');
|
|
|
|
assert.equal(stderr, '\n Error: mock stack null\n');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|