-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
onerror.js
85 lines (65 loc) · 1.83 KB
/
onerror.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const assert = require('assert');
const Koa = require('../..');
const mm = require('mm');
describe('app.onerror(err)', () => {
afterEach(mm.restore);
it('should throw an error if a non-error is given', () => {
const app = new Koa();
assert.throws(() => {
app.onerror('foo');
}, TypeError, 'non-error thrown: foo');
});
it('should accept errors coming from other scopes', () => {
const ExternError = require('vm').runInNewContext('Error');
const app = new Koa();
const error = Object.assign(new ExternError('boom'), {
status: 418,
expose: true
});
assert.doesNotThrow(() => app.onerror(error));
});
it('should do nothing if status is 404', () => {
const app = new Koa();
const err = new Error();
err.status = 404;
let called = false;
mm(console, 'error', () => { called = true; });
app.onerror(err);
assert(!called);
});
it('should do nothing if .silent', () => {
const app = new Koa();
app.silent = true;
const err = new Error();
let called = false;
mm(console, 'error', () => { called = true; });
app.onerror(err);
assert(!called);
});
it('should log the error to stderr', () => {
const app = new Koa();
app.env = 'dev';
const err = new Error();
err.stack = 'Foo';
let msg = '';
mm(console, 'error', input => {
if (input) msg = input;
});
app.onerror(err);
assert(msg === '\n Foo\n');
});
it('should use err.toString() instad of err.stack', () => {
const app = new Koa();
app.env = 'dev';
const err = new Error('mock stack null');
err.stack = null;
app.onerror(err);
let msg = '';
mm(console, 'error', input => {
if (input) msg = input;
});
app.onerror(err);
assert(msg === '\n Error: mock stack null\n');
});
});